我有一个包含start_date列(EV200_EVT_START_DATE)和end_date列(EV200_EVT_END_DATE)的事件表。活动可能持续一个多月。
我每年的每一天都有一张桌子(我认为可以提供帮助)。
我希望能够按月分配收入 - 所以基本上我认为我需要将每个事件与每个相应的月/年重叠。然后,我需要计算事件运行的总天数,并将“加入”月份中的事件天数除以。
Event Table (EV200_EVENT_MASTER)
===
EV200_EVT_START_DATE,EV200_EVT_END_DATE,EV200_EVT_ID
15/01/2012,17/01/2012,1
15/01/2012,15/02/2012,2
15/01/2012,15/03/2012,3
Calendar Table (EV001_MSTR_DATE_SCHED)
===
EV001_ISO_DATE
01/01/2012
02/01/2012
...
01/01/2021
02/01/2021
Output (something like)
===
EV200_EVT_START_DATE,EV200_EVT_END_DATE,EV200_EVT_ID,Year,Month,Days,Split
15/01/2012,17/01/2012,1,2012,1,2,1
15/01/2012,15/02/2012,2,2012,1,17,0.55
15/01/2012,15/02/2012,2,2012,2,14,0.45
15/01/2012,15/03/2012,3,2012,1,17,0.28
15/01/2012,15/03/2012,3,2012,2,29,0.48
15/01/2012,15/03/2012,3,2012,3,14,0.23
答案 0 :(得分:3)
给出
那么这应该给你想要的东西。但是,如果某个会计师涉及某个地方,他们可能会有关于何时确认收入的规则......
Select
Year(d.ID) As Year,
Month(d.ID) As Month,
Sum(e.Revenue/Cast((e.EndDate - e.StartDate + 1) As Decimal)) As RevenuePerMonth
From
Event e
Inner Join
Days d
On e.StartDate <= d.ID And e.EndDate >= d.ID
Group By
Year(d.ID),
Month(d.ID)
Order By
1, 2
编辑
这次测试:
Select
e.Ev200_Evt_Start_Date,
e.Ev200_Evt_End_Date,
e.Ev200_Evt_Id,
Year(d.Ev001_Iso_Date) As Year,
Month(d.Ev001_Iso_Date) As Month,
e.Ev200_Evt_End_Date - e.Ev200_Evt_Start_Date + 1 As Days,
Cast(Count(*) As Decimal)/Cast(e.Ev200_Evt_End_Date - e.Ev200_Evt_Start_Date + 1 As Decimal) As Split
From
Ev200_Event_Master e
Inner Join
Ev001_Mstr_Date_Sched d
On e.Ev200_Evt_Start_Date <= d.Ev001_Iso_Date And e.Ev200_Evt_End_Date >= d.Ev001_Iso_Date
Group By
e.Ev200_Evt_Start_Date,
e.Ev200_Evt_End_Date,
e.Ev200_Evt_Id,
Year(d.Ev001_Iso_Date),
Month(d.Ev001_Iso_Date),
e.Ev200_Evt_End_Date - e.Ev200_Evt_Start_Date + 1