2012-01-02 52 views
0

以下是顯示給定日期之間的星期數的腳本。將周顯示爲列

SET DATEFIRST 1  
SELECT ta.account, ta.customer, SUM(amount), DATEPART(ww,ta.dt) WeekNumber 
FROM tablename ta 
WHERE dt >= '12/01/2011 00:00:00' 
     and dt < '12/29/2011 00:00:00' 
GROUP BY ta.account, ta.customer, DATEPART(ww,ta.dt) 

如何在結果中顯示diff差異列作爲diff列。任何建議都會有所幫助。

樣品O/P爲上面是:

Date Account   Customer TotalSeconds Amount WeekNumber 

2011-11-01 xx0918252 198303792R 394   2.99 45 
2011-11-08 xx1006979 200100567G 92   0.16 46 
2011-11-15 xx1005385 A6863744I 492   1.275 47 
2011-11-21 xx1012872 D7874694G 770   0.52 48 
2011-11-28 xx1006419 C7112151H 1904  2.64 49 
2011-11-28 xx1006420 G7378945A 77   0.3  49 

我想像O/P:

Date  Account Customer TotalSeconds Amount WeekNumber45 WeekNumber46 WeekNumber47 WeekNumber8 WeekNumber49 

和它們的相應的數據。希望你明白我的問題。提前致謝。

大家好,感謝您的建議和幫助。最後,我得到了我暫時想要的結果。我仍然認爲這是硬編碼。有沒有更好的解決方案。提前致謝。我的代碼如下:

SELECT ta.account, ta.customer, 
isnull(SUM(CASE WHEN DATEPART(ww,ta.dt) = '49' THEN amount END),0) AS "Week49", 

isnull(SUM(CASE WHEN DATEPART(ww,ta.dt) = '50' THEN amount END),0) AS "Week50", 

isnull(SUM(CASE WHEN DATEPART(ww,ta.dt) = '51' THEN amount END),0) AS "Week51", 

isnull(SUM(CASE WHEN DATEPART(ww,ta.dt) = '52' THEN amount END),0) AS "Week52", 

isnull(SUM(CASE WHEN DATEPART(ww,ta.dt) = '53' THEN amount END),0) AS "Week53", 

FROM (
select * from tablename 
where dt >= '12/01/2011 00:00:00' and dt <= '12/31/2011 00:00:00' 
) ta 
group by ta.account, ta.customer 

回答

0

看看PIVOT功能。

Tsql pivot command

+1

關鍵點的問題是您必須能夠提前命名列。如果sandysmith願意硬編碼的日期,這隻會工作。 – 2012-01-02 05:33:00

+0

@傑拉德:你的權利。這不是我想要的,因爲輸入日期可能有所不同,而且我們也不提前提供週數。 – sandysmith 2012-01-02 05:48:38

1

首先,我會把你的結果在臨時表中,便於以後計算。讓我們想象一下以下CTE是你的結果:

if object_id('tempdb..#tab') is not null drop table #tab 

;with cte (Date,Account,Customer,TotalSeconds,Amount,WeekNumber) as (
    select cast('20111101' as datetime),'xx0918252','198303792R',394,2.99,45 union all 
    select '20111108','xx1006979','200100567G',92,0.16,46 union all 
    select '20111115','xx1005385','A6863744I',492,1.275,47 union all 
    select '20111121','xx1012872','D7874694G',770,0.52,48 union all 
    select '20111128','xx1006419','C7112151H',1904,2.64,49 union all 
    select '20111128','xx1006420','G7378945A',77,0.3,49 
) 
select * into #tab from cte 

現在你的計算數據是#tab表,下面的查詢返回轉動表對於那些weeknumber S:

select date, account, customer, totalSeconds, amount, [45], [46], [47], [48], [49] from 
(
    select date, account, customer, totalSeconds, amount, weeknumber as weeknumber from #tab 
) src 
pivot 
(
    max(weeknumber) for weekNumber in ([45], [46], [47], [48], [49]) 
) pvt 

此查詢的動態版本看起來像這樣:

declare @sql nvarchar(max), @cols varchar(max) 

select @cols = coalesce(@cols + ',', '') + '[' + cast(weeknumber as varchar) + ']' 
from (select distinct weeknumber from #tab) t 
order by weeknumber 

set @sql = N' 
    select date, account, customer, totalSeconds, amount, ' + @cols + ' from 
    (
     select date, account, customer, totalSeconds, amount, weeknumber as weeknumber from #tab 
    ) src 
    pivot 
    (
     max(weeknumber) for weekNumber in (' + @cols + ') 
    ) pvt 
' 

exec sp_executesql @sql 

結果(在這兩種情況下):

date     account customer totalSeconds amount  45  46  47  48  49 
----------------------- --------- ---------- ------------ ---------- ------ ------ ------ ------ ------ 
2011-11-01 00:00:00.000 xx0918252 198303792R 394   2.990  45  NULL NULL NULL NULL 
2011-11-08 00:00:00.000 xx1006979 200100567G 92   0.160  NULL 46  NULL NULL NULL 
2011-11-15 00:00:00.000 xx1005385 A6863744I 492   1.275  NULL NULL 47  NULL NULL 
2011-11-21 00:00:00.000 xx1012872 D7874694G 770   0.520  NULL NULL NULL 48  NULL 
2011-11-28 00:00:00.000 xx1006419 C7112151H 1904   2.640  NULL NULL NULL NULL 49 
2011-11-28 00:00:00.000 xx1006420 G7378945A 77   0.300  NULL NULL NULL NULL 49 
+0

嗨米歇爾,謝謝你的努力。由於我是SQL新手,我發現理解你的查詢很困難。但是,仍然在努力。再次感謝。 – sandysmith 2012-01-05 06:15:03

+0

@sandysmith最重要的是['pivot'](http://msdn.microsoft.com/en-us/library/ms177410.aspx) - 它用於第一個查詢,當知道有限的時候這很容易編寫您嘗試轉置爲列的值的數量 - 例如在此特定情況下的「weeknumber」。在寫查詢時,如果不清楚有多少列以及想要獲得哪個列,並且在第二個查詢中解決,那麼該查詢會生成列名* only *,以便處理'weeknumber'列中的值。 – 2012-01-05 06:55:39

+0

最重要的是['pivot'](http://msdn.microsoft.com/zh-cn/library/ms177410.aspx) - 它用於第一個查詢,當知道有限數量的您嘗試轉置爲列的值 - 例如在此特定情況下的「weeknumber」。在寫查詢時,如果不清楚有多少列以及想要獲得哪個列,並且在第二個查詢中解決,那麼該查詢會生成列名* only *,以便處理'weeknumber'列中的值。 – 2012-01-05 06:56:27