Populating a list of dates from a few date ranges in SQL server

半城伤御伤魂 提交于 2019-12-11 13:02:38

问题


I have a long list of accounts with date ranges and costs, here are two rows for example:

Account    Start_date   End_date    Cost
Megadeal    11/6/2015   13/6/2015   1000$ 
Superstar   12/8/2014   14/8/2014   2000$ 

And I need to populate a list with all the dates in each range, one row for each day, and each day with the name of the account and cost. The result should look something like that:

Megadeal    11/6/2015   1000
Megadeal    12/6/2015   1000
Megadeal    13/6/2015   1000
Superstar   12/8/2014   2000
Superstar   13/8/2014   2000
Superstar   14/8/2014   2000

Meaning to populate the list of dates from a different range every time (different start date and end date).

Any advice?


回答1:


You can use a Tally Table to generate the dates:

DECLARE @maxDiff INT;

SELECT @maxDiff = MAX(DATEDIFF(DAY, Start_Date, End_Date)) FROM tbl;

WITH E1(N) AS( -- 10 ^ 1 = 10 rows
    SELECT 1 FROM(VALUES (1),(1),(1),(1),(1),(1),(1),(1),(1),(1))t(N)
),
E2(N) AS(SELECT 1 FROM E1 a CROSS JOIN E1 b), -- 10 ^ 2 = 100 rows
E4(N) AS(SELECT 1 FROM E2 a CROSS JOIN E2 b), -- 10 ^ 4 = 10,000 rows
E8(N) AS(SELECT 1 FROM E4 a CROSS JOIN E4 b), -- 10 ^ 8 = 10,000,000 rows
CteTally(N) AS(
    SELECT TOP(@maxDiff + 1) ROW_NUMBER() OVER(ORDER BY(SELECT NULL))
    FROM E8
)
SELECT
    t.Account,
    Date = DATEADD(DAY, ct.N - 1, t.Start_Date),
    t.Cost
FROM tbl t
CROSS JOIN CteTally ct
WHERE DATEADD(DAY, ct.N - 1, t.Start_Date) <= t.End_Date
ORDER BY t.Account, Date;


来源:https://stackoverflow.com/questions/34986507/populating-a-list-of-dates-from-a-few-date-ranges-in-sql-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!