夏令时计算。我们确实有一个非常丑陋的工作模式。
见过这个 - SQL - Ugly combination of GROUP BY and COALESCE
如果我们可以设置变量..这不起作用 - 也删除了计数(*)以说明我们看到的问题..
Declare @subtractor int;
select top 10 case
when LOGGED_TIME between
case DATEPART(dw,CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))
when 1 then dateadd(hh,-4,CAST('03/08/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))--2am minus 6 hours for conversion from UST going to DST
else dateadd(hh,-4,DATEADD(d,15-datepart(dw,CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)),CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)))
end
and
case DATEPART(dw,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))
when 1 then dateadd(hh,-3,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)) --2am minus 5 hours for conversion from UST going from DST
else dateadd(hh,-3,DATEADD(d,8-datepart(dw,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)),CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)))
end
then
Set @subtractor = 5;
else
Set @subtractor = 6;
end
CAST(DATEADD(HH, -@subtractor,LOGGED_TIME) AS date) AS ForDate
, txtW=DATENAME(WEEKDAY, DATEADD(HH, -@subtractor,LOGGED_TIME))
, intW=DATEPART(WEEKDAY, DATEADD(HH, -@subtractor,LOGGED_TIME))
, intH=DATEPART(HOUR, DATEADD(HH, -@subtractor,LOGGED_TIME))
from SUPPORT_DATABASE.dbo.LOG (nolock)
这确实有用..然而,它是一个非常非常丑陋的GROUP BY ..有没有办法清理它?
select top 10
case
when LOGGED_TIME between
case DATEPART(dw,CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))
when 1 then dateadd(hh,-4,CAST('03/08/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))--2am minus 6 hours for conversion from UST going to DST
else dateadd(hh,-4,DATEADD(d,15-datepart(dw,CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)),CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)))
end
and
case DATEPART(dw,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))
when 1 then dateadd(hh,-3,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)) --2am minus 5 hours for conversion from UST going from DST
else dateadd(hh,-3,DATEADD(d,8-datepart(dw,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)),CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)))
end
then CAST(dateadd(hh,-5,LOGGED_TIME) AS date) -- DST
else CAST(dateadd(hh,-6,LOGGED_TIME) AS date) -- DST
end as ForDate,
case
when LOGGED_TIME between
case DATEPART(dw,CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))
when 1 then dateadd(hh,-4,CAST('03/08/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))--2am minus 6 hours for conversion from UST going to DST
else dateadd(hh,-4,DATEADD(d,15-datepart(dw,CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)),CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)))
end
and
case DATEPART(dw,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))
when 1 then dateadd(hh,-3,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)) --2am minus 5 hours for conversion from UST going from DST
else dateadd(hh,-3,DATEADD(d,8-datepart(dw,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)),CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)))
end
then DATENAME(WEEKDAY,dateadd(hh,-5,LOGGED_TIME)) -- DST
else DATENAME(WEEKDAY,dateadd(hh,-6,LOGGED_TIME)) -- DST
end as txtW,
case
when LOGGED_TIME between
case DATEPART(dw,CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))
when 1 then dateadd(hh,-4,CAST('03/08/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))--2am minus 6 hours for conversion from UST going to DST
else dateadd(hh,-4,DATEADD(d,15-datepart(dw,CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)),CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)))
end
and
case DATEPART(dw,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))
when 1 then dateadd(hh,-3,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)) --2am minus 5 hours for conversion from UST going from DST
else dateadd(hh,-3,DATEADD(d,8-datepart(dw,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)),CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)))
end
then DATENAME(WEEKDAY,dateadd(hh,-5,LOGGED_TIME)) -- DST
else DATENAME(WEEKDAY,dateadd(hh,-6,LOGGED_TIME)) -- DST
end as intW,
case
when LOGGED_TIME between
case DATEPART(dw,CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))
when 1 then dateadd(hh,-4,CAST('03/08/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))--2am minus 6 hours for conversion from UST going to DST
else dateadd(hh,-4,DATEADD(d,15-datepart(dw,CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)),CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)))
end
and
case DATEPART(dw,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))
when 1 then dateadd(hh,-3,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)) --2am minus 5 hours for conversion from UST going from DST
else dateadd(hh,-3,DATEADD(d,8-datepart(dw,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)),CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)))
end
then DATEPART(HOUR,dateadd(hh,-5,LOGGED_TIME)) -- DST
else DATEPART(HOUR,dateadd(hh,-6,LOGGED_TIME)) -- DST
end as intH
, COUNT(*) AS Totals
FROM SUPPORT_DATABASE.dbo.LOG WITH (nolock)
GROUP BY case
when LOGGED_TIME between
case DATEPART(dw,CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))
when 1 then dateadd(hh,-4,CAST('03/08/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))--2am minus 6 hours for conversion from UST going to DST
else dateadd(hh,-4,DATEADD(d,15-datepart(dw,CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)),CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)))
end
and
case DATEPART(dw,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))
when 1 then dateadd(hh,-3,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)) --2am minus 5 hours for conversion from UST going from DST
else dateadd(hh,-3,DATEADD(d,8-datepart(dw,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)),CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)))
end
then CAST(dateadd(hh,-5,LOGGED_TIME) AS date) -- DST
else CAST(dateadd(hh,-6,LOGGED_TIME) AS date) -- DST
end,
case
when LOGGED_TIME between
case DATEPART(dw,CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))
when 1 then dateadd(hh,-4,CAST('03/08/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))--2am minus 6 hours for conversion from UST going to DST
else dateadd(hh,-4,DATEADD(d,15-datepart(dw,CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)),CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)))
end
and
case DATEPART(dw,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))
when 1 then dateadd(hh,-3,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)) --2am minus 5 hours for conversion from UST going from DST
else dateadd(hh,-3,DATEADD(d,8-datepart(dw,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)),CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)))
end
then DATENAME(WEEKDAY,dateadd(hh,-5,LOGGED_TIME)) -- DST
else DATENAME(WEEKDAY,dateadd(hh,-6,LOGGED_TIME)) -- DST
end
,
case
when LOGGED_TIME between
case DATEPART(dw,CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))
when 1 then dateadd(hh,-4,CAST('03/08/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))--2am minus 6 hours for conversion from UST going to DST
else dateadd(hh,-4,DATEADD(d,15-datepart(dw,CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)),CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)))
end
and
case DATEPART(dw,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))
when 1 then dateadd(hh,-3,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)) --2am minus 5 hours for conversion from UST going from DST
else dateadd(hh,-3,DATEADD(d,8-datepart(dw,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)),CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)))
end
then DATEPART(WEEKDAY,dateadd(hh,-5,LOGGED_TIME)) -- DST
else DATEPART(WEEKDAY,dateadd(hh,-6,LOGGED_TIME)) -- DST
end,
case
when LOGGED_TIME between
case DATEPART(dw,CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))
when 1 then dateadd(hh,-4,CAST('03/08/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))--2am minus 6 hours for conversion from UST going to DST
else dateadd(hh,-4,DATEADD(d,15-datepart(dw,CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)),CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)))
end
and
case DATEPART(dw,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))
when 1 then dateadd(hh,-3,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)) --2am minus 5 hours for conversion from UST going from DST
else dateadd(hh,-3,DATEADD(d,8-datepart(dw,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)),CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)))
end
then DATEPART(HOUR,dateadd(hh,-5,LOGGED_TIME)) -- DST
else DATEPART(HOUR,dateadd(hh,-6,LOGGED_TIME)) -- DST
end
答案 0 :(得分:2)
你可以将它包装在cte中,这样你就可以GROUP BY
别名:
;WITH cte AS ( select
case
when LOGGED_TIME between
case DATEPART(dw,CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))
when 1 then dateadd(hh,-4,CAST('03/08/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))--2am minus 6 hours for conversion from UST going to DST
else dateadd(hh,-4,DATEADD(d,15-datepart(dw,CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)),CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)))
end
and
case DATEPART(dw,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))
when 1 then dateadd(hh,-3,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)) --2am minus 5 hours for conversion from UST going from DST
else dateadd(hh,-3,DATEADD(d,8-datepart(dw,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)),CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)))
end
then CAST(dateadd(hh,-5,LOGGED_TIME) AS date) -- DST
else CAST(dateadd(hh,-6,LOGGED_TIME) AS date) -- DST
end as ForDate,
case
when LOGGED_TIME between
case DATEPART(dw,CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))
when 1 then dateadd(hh,-4,CAST('03/08/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))--2am minus 6 hours for conversion from UST going to DST
else dateadd(hh,-4,DATEADD(d,15-datepart(dw,CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)),CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)))
end
and
case DATEPART(dw,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))
when 1 then dateadd(hh,-3,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)) --2am minus 5 hours for conversion from UST going from DST
else dateadd(hh,-3,DATEADD(d,8-datepart(dw,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)),CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)))
end
then DATENAME(WEEKDAY,dateadd(hh,-5,LOGGED_TIME)) -- DST
else DATENAME(WEEKDAY,dateadd(hh,-6,LOGGED_TIME)) -- DST
end as txtW,
case
when LOGGED_TIME between
case DATEPART(dw,CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))
when 1 then dateadd(hh,-4,CAST('03/08/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))--2am minus 6 hours for conversion from UST going to DST
else dateadd(hh,-4,DATEADD(d,15-datepart(dw,CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)),CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)))
end
and
case DATEPART(dw,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))
when 1 then dateadd(hh,-3,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)) --2am minus 5 hours for conversion from UST going from DST
else dateadd(hh,-3,DATEADD(d,8-datepart(dw,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)),CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)))
end
then DATENAME(WEEKDAY,dateadd(hh,-5,LOGGED_TIME)) -- DST
else DATENAME(WEEKDAY,dateadd(hh,-6,LOGGED_TIME)) -- DST
end as intW,
case
when LOGGED_TIME between
case DATEPART(dw,CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))
when 1 then dateadd(hh,-4,CAST('03/08/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))--2am minus 6 hours for conversion from UST going to DST
else dateadd(hh,-4,DATEADD(d,15-datepart(dw,CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)),CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)))
end
and
case DATEPART(dw,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))
when 1 then dateadd(hh,-3,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)) --2am minus 5 hours for conversion from UST going from DST
else dateadd(hh,-3,DATEADD(d,8-datepart(dw,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)),CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)))
end
then DATEPART(HOUR,dateadd(hh,-5,LOGGED_TIME)) -- DST
else DATEPART(HOUR,dateadd(hh,-6,LOGGED_TIME)) -- DST
end as intH
FROM SUPPORT_DATABASE.dbo.LOG WITH (nolock))
SELECT *,COUNT(*) CT
FROM cte
GROUP BY ForDate, txtW, intW, intH
这是一种简单的清理方法,确保它也需要重构。
答案 1 :(得分:1)
你可以创建一个UDF:
CREATE FUNCTION dbo.YourFunctionName (@DateTime DATETIME)
RETURNS DATE
AS
BEGIN
RETURN
(
SELECT case
when @DateTime between
case DATEPART(dw,CAST('03/01/'+CAST (YEAR(@DateTime) as varchar(4)) as datetime))
when 1 then dateadd(hh,-4,CAST('03/08/'+CAST (YEAR(@DateTime) as varchar(4)) as datetime))--2am minus 6 hours for conversion from UST going to DST
else dateadd(hh,-4,DATEADD(d,15-datepart(dw,CAST('03/01/'+CAST (YEAR(@DateTime) as varchar(4)) as datetime)),CAST('03/01/'+CAST (YEAR(@DateTime) as varchar(4)) as datetime)))
end
and
case DATEPART(dw,CAST('11/01/'+CAST (YEAR(@DateTime) as varchar(4)) as datetime))
when 1 then dateadd(hh,-3,CAST('11/01/'+CAST (YEAR(@DateTime) as varchar(4)) as datetime)) --2am minus 5 hours for conversion from UST going from DST
else dateadd(hh,-3,DATEADD(d,8-datepart(dw,CAST('11/01/'+CAST (YEAR(@DateTime) as varchar(4)) as datetime)),CAST('11/01/'+CAST (YEAR(@DateTime) as varchar(4)) as datetime)))
end
then CAST(dateadd(hh,-5,@DateTime) AS date) -- DST
else CAST(dateadd(hh,-6,@DateTime) AS date) -- DST
end
);
END
然后您的查询就是:
SELECT dbo.YourFunctionName(LOGGED_TIME) AS ForDate,
DATENAME(WEEKDAY, dbo.YourFunctionName(LOGGED_TIME)) AS txtW
....
GROUP BY dbo.YourFunctionName(LOGGED_TIME);
或者,如果您永远不会重复使用此功能并且不想为此创建它,您可以将逻辑移至APPLY,然后引用您的新列:
SELECT fd.ForDate,
DATENAME(WEEKDAY, fd.ForDate) AS txtW
FROM ...
CROSS APPLY
(
SELECT case
when LOGGED_TIME between
case DATEPART(dw,CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))
when 1 then dateadd(hh,-4,CAST('03/08/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))--2am minus 6 hours for conversion from UST going to DST
else dateadd(hh,-4,DATEADD(d,15-datepart(dw,CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)),CAST('03/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)))
end
and
case DATEPART(dw,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime))
when 1 then dateadd(hh,-3,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)) --2am minus 5 hours for conversion from UST going from DST
else dateadd(hh,-3,DATEADD(d,8-datepart(dw,CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)),CAST('11/01/'+CAST (YEAR(LOGGED_TIME) as varchar(4)) as datetime)))
end
then CAST(dateadd(hh,-5,LOGGED_TIME) AS date) -- DST
else CAST(dateadd(hh,-6,LOGGED_TIME) AS date) -- DST
end as ForDate
) AS fd
GROUP BY fd.ForDate;
或者最后,你可以在外部查询中使用子查询和组。