[英]SQL Server 2008 get date range from Week of year
I have the below query which groups by week (Sun-Sat) and does the necessary calculation. 我在下面的查询中按周(周日至周六)分组并进行必要的计算。 the output obviously gives the week number of the year.
输出显然给出了一年中的第几周。 As a first step I can store this data in a table, then when I want to use this data I want to convert the week number of year to the actual date range.
首先,我可以将这些数据存储在一个表中,然后当我想使用此数据时,我想将年份的星期数转换为实际的日期范围。 Below is the query.
下面是查询。
SELECT
DATEPART(WW,aa.Time) ddtt ,bb.Nd ,'Percentages' Report
,case when SUM(ZZ) = 0 then 0 else convert(decimal(18,3),SUM((CCC+PSY))*100/SUM(ZZ)) end Cond1
,case when SUM(ZZ) = 0 then 0 else convert(decimal(18,3),SUM(USN)*100/SUM(ZZ)) end Cond2
FROM db2000.dbo.Table aa join db2000.dbo.List bb on aa.Device = bb.DeviceID
where aa.Time between '2013/12/15' AND '2014/1/15 23:00' and Nd like '_s1'
group by bb.Nd ,DATEPART(WW,aa.Time)
order by ddtt
The output of this query is 该查询的输出是
ddtt Nd Report Cond1 Cond2 1 21S Percentages 94.787 63.998 1 41S Percentages 94.592 63.473 1 61S Percentages 94.356 65.845 2 21S Percentages 93.802 64.594 2 41S Percentages 94.141 65.486 2 61S Percentages 93.849 66.144 3 21S Percentages 94.572 65.940 3 41S Percentages 95.123 67.261 3 61S Percentages 95.044 67.211 51 21S Percentages 94.042 65.245 51 41S Percentages 94.857 65.847 51 61S Percentages 94.036 67.019 52 21S Percentages 94.592 65.469 52 41S Percentages 95.071 66.159 52 61S Percentages 93.932 66.989 53 21S Percentages 94.786 65.391 53 41S Percentages 95.266 66.883 53 61S Percentages 94.526 67.504
I want the column ddtt with values to represent the actual dates, for eg. 我希望列ddtt的值代表实际日期,例如。 05/01/2014 - 11/01/2014.
2014年5月1日至2014年11月1日。 A separate query to accomplish this will be ok too.
一个单独的查询来完成此操作也是可以的。
To get the date from sunday to saturday given the week number you can use 要获得星期几到星期几的日期,可以使用
SELECT dateadd(dd, -datepart(wk, '2014-01-08') - 1
, dateadd(ww, @weeknum, '2014-01-01'))
, dateadd(dd, -datepart(wk, '2014-01-08') - 2
, dateadd(ww, @weeknum + 1, '2014-01-01'))
where @weeknum
is the week number, for the day of the week I used datepart(wk, '2014-01-08')
because using the first of January will always return 1, regardless of the real day of week. 其中
@weeknum
是星期数,对于我使用datepart(wk, '2014-01-08')
的星期几datepart(wk, '2014-01-08')
因为使用1月的第一天将始终返回1,而不考虑星期几。
You query will become 您的查询将成为
SELECT DATEADD(dd, -DATEPART(wk, '2014-01-08') - 1
, DATEADD(ww, DATEPART(WW,aa.Time), '2014-01-01'))
, DATEADD(dd, -DATEPART(wk, '2014-01-08') - 2
, DATEADD(ww, DATEPART(WW,aa.Time) + 1, '2014-01-01'))
, bb.Nd
, 'Percentages' Report
, CASE WHEN SUM(ZZ) = 0 THEN 0
ELSE convert(decimal(18,3),SUM((CCC+PSY))*100/SUM(ZZ))
END Cond1
, CASE WHEN SUM(ZZ) = 0 THEN 0
ELSE convert(decimal(18,3),SUM(USN)*100/SUM(ZZ))
END Cond2
FROM db2000.dbo.Table aa
JOIN db2000.dbo.List bb ON aa.Device = bb.DeviceID
WHERE aa.Time BETWEEN '2013/12/15' AND '2014/1/15 23:00' AND Nd LIKE '_s1'
GROUP BY bb.Nd ,DATEPART(WW,aa.Time)
ORDER BY ddtt
or something similar if you want to join the two date in a string. 或类似的内容(如果您想将两个日期连在一起)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.