2015-12-30 57 views
-2

我有一個表,我想按列顯示數據。我已經完成了一個星期,但在一個月的所有周裏都做不到。按星期組使用SQL Server 2008

在我的表我想組由每週ID數據,總結其

我的樣本數據是在這裏:SqlFiddle

樣品O/P

121212 1212 7646 45647 
+3

可能是我缺少的東西,但你如何期待你提供的表的輸出? –

+0

4個不同欄目的4周輸出@RahulTripathi –

+0

它會一直保持4周,或者是一個動態的時期,即最後4個整週? –

回答

0

檢查這

SELECT month 
    ,SUM(CASE WHEN day between 1 and 7 then Value END) as WEEK_1_VAL 
    ,SUM(CASE WHEN day between 8 and 14 then Value END) as WEEK_2_VAL 
    ,SUM(CASE WHEN day between 15 and 21 then Value END) as WEEK_3_VAL 
    ,SUM(CASE WHEN day between 22 and 28 then Value END) as WEEK_4_VAL 
    ,SUM(CASE WHEN day between 29 and 31 then Value END) as WEEK_5_VAL 
FROM sample GROUP BY month 
; 

輸出

month WEEK_1_VAL WEEK_2_VAL WEEK_3_VAL WEEK_4_VAL WEEK_5_VAL 
12  8   13   1   119.78  11.89 
+0

每週8天? –

+0

不,一週7天 –

+0

每週更換7天。 @DheerajPatnaik做這個答案。 – minatverma

0

做一個查詢,你按DATEPART(week, ...),這將產生每週一列,然後轉動該查詢,使行成列。