如何根据sql中的另一个表限制从表中读取的值

时间:2016-10-12 19:02:51

标签: sql sql-server

我正在使用sqlserver和C#。我必须在一个名为time_series的表的开始和结束时间之间以30分钟的增量填充一个下拉框。例如,从该表读取的日期将类似于上午10:00,上午10:30,上午11:00,上午11:30 ......下午5:00。我有另外两个表的start(start_time表)和结束时间(end_time表)具有特定的值。例如,如果start_time表的值为11:00 am,那么时间序列应该从上午11:00而不是上午10:00开始。同样,如果结束时间表的值为下午3:00,那么时间序列应该只到下午3:00而不是下午5:00。有没有办法在sql中做到这一点?我可以在C#中首先读取所有三个表,然后按start_time和end_time表的值限制时间序列。但是我想在没有C#的情况下在sql中进行。

由于

2 个答案:

答案 0 :(得分:1)

这将创建一个30分钟跳转列表,从给定时间开始,以给定目标日期的给定结束时间结束:

DECLARE @StartTime TIME={t'08:00:00'};
DECLARE @EndTime TIME={t'17:30:00'};

DECLARE @targetDate DATETIME={d'2016-10-12'};

WITH JumpsOf30 AS
(
    SELECT TOP 48 (ROW_NUMBER() OVER(ORDER BY(SELECT NULL))-1) * 30 AS Jump
    FROM sys.objects --you don't need more than 48, so the count of this table should suffice...
)
SELECT DATEADD(MINUTE,Jump,@targetDate+CAST(@StartTime AS datetime))
FROM JumpsOf30
WHERE  DATEADD(MINUTE,Jump,@targetDate+CAST(@StartTime AS datetime))<=@targetDate+CAST(@EndTime AS datetime)

答案 1 :(得分:1)

我使用TVF创建动态日期时间范围。它比CTE方法快得多。

Select *
      ,TimeString=Format(RetVal,'hh:mm:ss tt') 
 From  [dbo].[udf-Range-Date]('1900-01-01 10:00:00','1900-01-01 17:00:00','MI',30)
 Where Cast(RetVal as Time)>=cast(GetDate() as Time)

返回

RetSeq  RetVal                     TimeString   << Current time was 3:22pm
12      1900-01-01 15:30:00.000    03:30:00 PM
13      1900-01-01 16:00:00.000    04:00:00 PM
14      1900-01-01 16:30:00.000    04:30:00 PM
15      1900-01-01 17:00:00.000    05:00:00 PM

所需的UDF

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