简体   繁体   中英

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. 05/01/2014 - 11/01/2014. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM