Reputation: 37
I have a query that returns these results:
ID | From | To | Value
------ | --------- | ---------- | -------
8 | 1/1/2018 | 2/28/2018 | .03
8 | 3/1/2018 | 4/30/2018 | .04
9 | 1/1/2018 | 1/31/2018 | .05
What I need is this:
ID | Date | Value
------ | --------- | ----------
8 | 1/1/2018 | .03
8 | 2/1/2018 | .03
8 | 3/1/2018 | .04
8 | 4/1/2018 | .04
9 | 1/1/2018 | .05
I have researched and it seems like Cross Apply needs to be used here along with date table, but just not sure how to use this operator.
Thanks in advance!
Upvotes: 1
Views: 3378
Reputation: 5543
Since John Cappelletti mentioned calendar tables, and since I'm always looking for an excuse to extol the virtue of using them, I'll just point out that if you have a Calendar table then the solution is simply:
select T.id, S.date, T.value from YourTable T
cross apply
(
select date from Calendar where date between T.[from] and T.[to] and day_of_month = 1
) S order by T.id, S.date asc
Which yields:
id date value
------ ---------- -------
8 2018-01-01 0.03
8 2018-02-01 0.03
8 2018-03-01 0.04
8 2018-04-01 0.04
9 2018-01-01 0.05
And here's all it takes to create a Calendar table:
create table Calendar
(
id int primary key identity,
date datetime,
day_of_week as datepart(dw, date),
day_of_month as datepart(d, date),
month as datepart(m, date),
year as datepart(yy, date),
day_name as datename(dw, date)
--etc...
)
--and populate
declare @day datetime
set @day = '1/1/2000'
while @day <= '12/31/2100'
begin
insert Calendar select @day
set @day = dateadd(day, 1, @day)
end
Upvotes: 3
Reputation: 81970
One approach is using an ad-hoc tally table within the CROSS APPLY. A numbers or calendar table would do the trick as well.
Example
Select A.ID
,B.Date
,A.Value
From YourTable A
Cross Apply (
Select Top (DateDiff(MONTH,A.[From],A.[To])+1)
Date=DateAdd(MONTH,-1+Row_Number() Over (Order By (Select Null)),A.[From])
From master..spt_values n1
) B
Returns
EDIT - If Open to a UDF
I'll often use a UDF to create dynamic date/time ranges. It is faster than a recursive cte and it is parameter driven. You supply the date/time range, datepart, and increment.
Example of UDF
Select A.ID
,Date = cast(B.RetVal as date)
,A.Value
From YourTable A
Cross Apply [dbo].[udf-Range-Date](A.[From],A.[To],'MM',1) B
The UDF if Interested
CREATE FUNCTION [dbo].[udf-Range-Date] (@R1 datetime,@R2 datetime,@Part varchar(10),@Incr int)
Returns Table
Return (
with cte0(M) As (Select 1+Case @Part When 'YY' then DateDiff(YY,@R1,@R2)/@Incr When 'QQ' then DateDiff(QQ,@R1,@R2)/@Incr When 'MM' then DateDiff(MM,@R1,@R2)/@Incr When 'WK' then DateDiff(WK,@R1,@R2)/@Incr When 'DD' then DateDiff(DD,@R1,@R2)/@Incr When 'HH' then DateDiff(HH,@R1,@R2)/@Incr When 'MI' then DateDiff(MI,@R1,@R2)/@Incr When 'SS' then DateDiff(SS,@R1,@R2)/@Incr End),
cte1(N) As (Select 1 From (Values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) N(N)),
cte2(N) As (Select Top (Select M from cte0) Row_Number() over (Order By (Select NULL)) From cte1 a, cte1 b, cte1 c, cte1 d, cte1 e, cte1 f, cte1 g, cte1 h ),
cte3(N,D) As (Select 0,@R1 Union All Select N,Case @Part When 'YY' then DateAdd(YY, N*@Incr, @R1) When 'QQ' then DateAdd(QQ, N*@Incr, @R1) When 'MM' then DateAdd(MM, N*@Incr, @R1) When 'WK' then DateAdd(WK, N*@Incr, @R1) When 'DD' then DateAdd(DD, N*@Incr, @R1) When 'HH' then DateAdd(HH, N*@Incr, @R1) When 'MI' then DateAdd(MI, N*@Incr, @R1) When 'SS' then DateAdd(SS, N*@Incr, @R1) End From cte2 )
Select RetSeq = N+1
,RetVal = D
From cte3,cte0
Where D<=@R2
)
/*
Max 100 million observations -- Date Parts YY QQ MM WK DD HH MI SS
Syntax:
Select * from [dbo].[udf-Range-Date]('2016-10-01','2020-10-01','YY',1)
Select * from [dbo].[udf-Range-Date]('2016-01-01','2017-01-01','MM',1)
*/
Upvotes: 2