2017-07-12 92 views
0

我有輸出的SQL查詢這樣集團按周在SQL

7 2017-05-02 00:00:00.000 
96 2017-05-15 00:00:00.000 
79 2017-05-16 00:00:00.000 
310 2017-05-17 00:00:00.000 
76 2017-05-18 00:00:00.000 
83 2017-05-19 00:00:00.000 

我想他們按周。

理想輸出

7  Week 1 
644 Week 2 

如何像這樣的組可以理解的任何幫助。謝謝!

+0

您正在使用哪個'DBMS'? –

+0

@prdp我正在使用MSSQL - SSMS – beginnerdscientist

回答

2

假設你可以用一個星期的數據庫定義生活:

select datename(year, datecol) + '-' + datename(week, datecol) as yyyyw, 
     sum(col) as sumcol 
from t 
group by datename(year, datecol) + '-' + datename(week, datecol) 
order by min(datecol); 
+0

謝謝!這正是我希望得到的。 – beginnerdscientist

0

另一種選擇

Select SomeValue = sum(SomeValue) 
     ,WeekNr=concat('Week ',dense_rank() over (Order By datepart(week,SomeDate))) 
from YourTable 
Group By year(SomeDate),datepart(week,SomeDate) 

返回

SomeValue WeekNr 
7   Week 1 
644   Week 2