在具有特定时间的 where 子句上使用昨天日期
Use yesterday date on where clause with specific time
我想 select 一个使用昨天日期和特定时间的值。这是我的例子:
所以我想用昨天的日期替换昨天的变量,但在特定时间指定。
select Flow, Sum(Morning) Morning, Sum(PM) PM, Sum(Night) Night, Count(*) Total
from [dbo].[MISSION]
cross apply (values (Iif(QUELLE in ('Réception_14','Réception_21'),'Flow 1',
Iif(QUELLE in ('Réception_17','Réception_16'),'Flow 2','Flow3'))))f(Flow)
cross apply (values ( Convert(time, [START_DATE] )))v(T)
cross apply (
select
case when T >= **YESTERDAYDATE:'06:00:00'** and T < **YESTERDAYDATE:'11:00:00'** then 1 else 0 end Morning,
case when T >=**YESTERDAYDATE:'11:00:00'** and T < **YESTERDAYDATE:'22:00:00'** then 1 else 0 end PM,
case when T >=**YESTERDAYDATE:'22:00:00'** and T < **YESTERDAYDATE:'06:00:00'** then 1 else 0 end Night
)c
group by Flow
谢谢
不要将你的 START_DATE
投射或转换为时间,否则你将无法像 T >= **YESTERDAYDATE:'06:00:00'
那样进行比较
cross apply
(
values ( [START_DATE] )
) v (T)
你需要昨天和今天午夜的日期00:00:00
cross apply
(
values (convert(datetime, convert(date, getdate())),
convert(datetime, convert(date, getdate() - 1)))
) dates (today, yesterday)
通过以上,你可以获得各种日期时间,如YESTERDAYDATE:'06:00:00'
、YESTERDAYDATE:'22:00:00'
cross apply
(
values (dateadd(hour, 6, yesterday),
dateadd(hour, 11, yesterday),
dateadd(hour, 22, yesterday),
dateadd(hour, 6, today))
) dt (y6, y11, y22, t6)
然后最后
cross apply
(
select case when T >= y6 and T < y11 then 1 else 0 end Morning,
case when T >= y11 and T < y22 then 1 else 0 end PM,
case when T >= y22 and T < t6 then 1 else 0 end Night
) c
注意:最后一个 CASE
表达式 Night
应该是从昨天 22:00
到今天 06:00
我想 select 一个使用昨天日期和特定时间的值。这是我的例子: 所以我想用昨天的日期替换昨天的变量,但在特定时间指定。
select Flow, Sum(Morning) Morning, Sum(PM) PM, Sum(Night) Night, Count(*) Total
from [dbo].[MISSION]
cross apply (values (Iif(QUELLE in ('Réception_14','Réception_21'),'Flow 1',
Iif(QUELLE in ('Réception_17','Réception_16'),'Flow 2','Flow3'))))f(Flow)
cross apply (values ( Convert(time, [START_DATE] )))v(T)
cross apply (
select
case when T >= **YESTERDAYDATE:'06:00:00'** and T < **YESTERDAYDATE:'11:00:00'** then 1 else 0 end Morning,
case when T >=**YESTERDAYDATE:'11:00:00'** and T < **YESTERDAYDATE:'22:00:00'** then 1 else 0 end PM,
case when T >=**YESTERDAYDATE:'22:00:00'** and T < **YESTERDAYDATE:'06:00:00'** then 1 else 0 end Night
)c
group by Flow
谢谢
不要将你的 START_DATE
投射或转换为时间,否则你将无法像 T >= **YESTERDAYDATE:'06:00:00'
cross apply
(
values ( [START_DATE] )
) v (T)
你需要昨天和今天午夜的日期00:00:00
cross apply
(
values (convert(datetime, convert(date, getdate())),
convert(datetime, convert(date, getdate() - 1)))
) dates (today, yesterday)
通过以上,你可以获得各种日期时间,如YESTERDAYDATE:'06:00:00'
、YESTERDAYDATE:'22:00:00'
cross apply
(
values (dateadd(hour, 6, yesterday),
dateadd(hour, 11, yesterday),
dateadd(hour, 22, yesterday),
dateadd(hour, 6, today))
) dt (y6, y11, y22, t6)
然后最后
cross apply
(
select case when T >= y6 and T < y11 then 1 else 0 end Morning,
case when T >= y11 and T < y22 then 1 else 0 end PM,
case when T >= y22 and T < t6 then 1 else 0 end Night
) c
注意:最后一个 CASE
表达式 Night
应该是从昨天 22:00
到今天 06:00