声明Startdate和Enddate后,子查询返回的值超过1

时间:2017-08-26 02:29:22

标签: sql sql-server database tsql

我只想在开始日期和结束日期之间声明,我有这样的SQL代码:

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);

如果我只有单个数据行,则此代码有效,如下所示: Database condition 1

但如果有多个数据行,例如:Database condition 2

显示错误:

  

子查询返回的值超过1。当子查询遵循=,!=,&lt;,&lt; =,&gt;,&gt; =或子查询用作表达式时,不允许这样做。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

我认为这可能就是你要找的东西。

借助ad-hoc计数表和CROSS APPLY。理货/日历表也可以解决这个问题。

不完全清楚您想要的结果,因此我添加了您的来源的所有字段... Select A.*

示例

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

<强>返回

enter image description here

  

编辑 - 如果对表值函数感兴趣

我经常使用UDF来创建动态日期/时间范围。它比递归CTE和参数驱动更快。

您提供日期时间范围,所需的日期部分和增量。

使用UDF方法,查询将如下所示:

Select A.* 
      ,Date = cast(B.RetVal as date)
 From  @TB_BlockTanggal A
 Cross Apply [dbo].[udf-Range-Date]([StartDateBlock],[EndDateBlock],'DD',1) B

感兴趣的UDF

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) 
*/