Reputation: 41
I just want declare between from start date and end date and I have SQL code like this:
DECLARE @DateFrom date, @DateTo date;
SET @DateFrom='10-15-2017';
SET @DateTo='10-20-2017';
WITH TB_BlockDate(date) AS
( SELECT @DateFrom
FROM [dbo].[TB_BlockTanggal]
WHERE Kode_Properti ='KP0062'
UNION ALL SELECT DateAdd(DAY,1,TB_BlockDate.date)
FROM TB_BlockDate
WHERE TB_BlockDate.date < @DateTo)
SELECT date
FROM TB_BlockDate OPTION (MAXRECURSION 32767);
This code works if i just have single data row, like this: Database condition 1
But if have multiple data rows, like this: Database condition 2
It shows error:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
How can I resolve this?
Upvotes: 0
Views: 187
Reputation: 81990
I think this may be what you are looking for.
With the help of an ad-hoc tally table and a CROSS APPLY. A tally/calendar table would do the trick as well.
Not exactly clear on your desired results, so I included all fields from your source ... Select A.*
Example
Declare @TB_BlockTanggal Table ([BlockID] varchar(50),[Kode_Properti] varchar(50),[KodeMember] varchar(50),[StartDateBlock] date,[EndDateBlock] date)
Insert Into @TB_BlockTanggal Values
(1,'KP0062','KM0005','10/10/2017','10/15/2017')
,(8,'KP0066','KM0005','10/17/2017','10/20/2017')
Select A.*
,B.*
From @TB_BlockTanggal A
Cross Apply (
Select Top (DateDiff(DAY,[StartDateBlock],[EndDateBlock])+1)
D=DateAdd(DAY,-1+Row_Number() Over (Order By (Select Null)),[StartDateBlock])
From master..spt_values n1,master..spt_values n2
) B
Returns
EDIT - If Interested in a Table-Valued Function
I'll often use a UDF to create dynamic date/time ranges. It is faster than a recursive CTE and parameter driven.
You supply the datetime range, desired datepart and increment.
Using the UDF approach, the query would look like this:
Select A.*
,Date = cast(B.RetVal as date)
From @TB_BlockTanggal A
Cross Apply [dbo].[udf-Range-Date]([StartDateBlock],[EndDateBlock],'DD',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