将周显示为列

时间:2012-01-02 05:20:43

标签: sql sql-server sql-server-2005 tsql

以下是显示给定日期之间的周数的脚本。

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

3 个答案:

答案 0 :(得分: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的透视表:

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

答案 1 :(得分:0)

看看PIVOT功能。

Tsql pivot command

答案 2 :(得分:0)