将“to-date”和“from-date”转换为连续的日历月增量。

huangapple go评论52阅读模式
英文:

Transpose to-date and from-date to consecutive calendar month increments

问题

以下是您要翻译的内容:

"我有这样的表格

人员 状态 起始日期 结束日期
1 A 2023-01-01 2023-03-31
1 B 2023-04-01 3999-12-31

我还有另一个按月汇总的余额表格。

人员 期间
1 2023-01-01
1 2023-02-01
1 2023-03-01
1 2023-04-01
1 2023-05-01

有什么聪明的方法可以在月增量上连接状态值,使其显示如下

人员 状态 期间
1 A 2023-01-01
1 A 2023-02-01
1 A 2023-03-01
1 B 2023-04-01
1 B 2023-05-01

期待您的想法!

我尝试了一些不同的连接方式,但无法做对。我相信有一种聪明的方法可以做到这一点。"

英文:

I have a table like this

Person Status FromDate ToDate
1 A 2023-01-01 2023-03-31
1 B 2023-04-01 3999-12-31

I also have another balance table with aggregates on a monthly basis.

Person Value Period
1 value 2023-01-01
1 value 2023-02-01
1 value 2023-03-01
1 value 2023-04-01
1 value 2023-05-01

What would be a smart way to join in the status values on a monthly increment so it appears as follows

Person Status Period
1 A 2023-01-01
1 A 2023-02-01
1 A 2023-03-01
1 B 2023-04-01
1 B 2023-05-01

Keen on thoughts!

I have tried some different joins but cant get it right. I am sure there is a smart way of doing this.

答案1

得分: 1

你可以使用 inner join 来实现:

select t.Person, t.Status, b.Period
from mytable t
inner join balance b on b.Period between t.FromDate and t.ToDate

结果:

Person	Status	Period
1	    A	    2023-01-01
1	    A	    2023-02-01
1	    A	    2023-03-01
1	    B	    2023-04-01
1	    B	    2023-05-01

演示示例

英文:

You can do it using inner join :

select t.Person, t.Status, b.Period
from mytable t
inner join balance b on b.Period between t.FromDate and t.ToDate

Result :

Person	Status	Period
1	    A	    2023-01-01
1	    A	    2023-02-01
1	    A	    2023-03-01
1	    B	    2023-04-01
1	    B	    2023-05-01

Demo here

huangapple
  • 本文由 发表于 2023年5月10日 23:04:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220011.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定