我们如何使用PIVOT进行以下转换?

时间:2018-12-04 15:59:55

标签: sql-server tsql

我有以下

enter image description here

使用以下查询生成

declare @t table(Bucket varchar(50), [RollBack] int, [Stabilized] int, [RollForward] int,[Normalized] int)

insert into @t 
    select 'Bucket >6',0,0,100,0 union all
    select 'Bucket 1',0,0,200,0 union all
    select 'Bucket 2',0,0,100,0 union all
    select 'Bucket 3',0,0,100,0 union all
    select 'Bucket 4',0,0,100,0 union all
    select 'Bucket 5',0,0,100,0 union all
    select 'Bucket 6',0,0,100,0

select * from @t

我正在寻找下面的转换

enter image description here

到目前为止我的尝试是

select a as Activities,

    sum(case when b.Bucket='Bucket 0' then 
                    case when a='RollBack' then [RollBack] 
                         when a='Stabilized' then Stabilized
                         when a='RollForward' then RollForward
                         when a='Normalized' then Normalized                         
                    end else 0 end) as [Bucket 0],

    sum(case when b.Bucket='Bucket 1' then 
                    case when a='RollBack' then [RollBack] 
                         when a='Stabilized' then Stabilized
                         when a='RollForward' then RollForward
                         when a='Normalized' then Normalized                         
                    end else 0 end) as [Bucket 1],

    sum(case when b.Bucket='Bucket 2' then 
                    case when a='RollBack' then [RollBack] 
                         when a='Stabilized' then Stabilized
                         when a='RollForward' then RollForward
                         when a='Normalized' then Normalized                         
                    end else 0 end) as [Bucket 2],

    sum(case when b.Bucket='Bucket 3' then 
                    case when a='RollBack' then [RollBack] 
                         when a='Stabilized' then Stabilized
                         when a='RollForward' then RollForward
                         when a='Normalized' then Normalized                         
                    end else 0 end) as [Bucket 3],

    sum(case when b.Bucket='Bucket 4' then 
                    case when a='RollBack' then [RollBack] 
                         when a='Stabilized' then Stabilized
                         when a='RollForward' then RollForward
                         when a='Normalized' then Normalized                         
                    end else 0 end) as [Bucket 4],

    sum(case when b.Bucket='Bucket 5' then 
                    case when a='RollBack' then [RollBack] 
                         when a='Stabilized' then Stabilized
                         when a='RollForward' then RollForward
                         when a='Normalized' then Normalized                         
                    end else 0 end) as [Bucket 5],

    sum(case when b.Bucket='Bucket 6' then 
                    case when a='RollBack' then [RollBack] 
                         when a='Stabilized' then Stabilized
                         when a='RollForward' then RollForward
                         when a='Normalized' then Normalized                         
                    end else 0 end) as [Bucket 6],

    sum(case when b.Bucket='Bucket >6' then 
                    case when a='RollBack' then [RollBack] 
                         when a='Stabilized' then Stabilized
                         when a='RollForward' then RollForward
                         when a='Normalized' then Normalized                         
                    end else 0 end) as [Bucket >6]

    from (values ('RollBack'),('Stabilized'),('RollForward'),('Normalized')) t(a)
            cross join @t b
            group by a  

这给了我

enter image description here

1 个答案:

答案 0 :(得分:1)

我将使用make进行休息操作:

APPLY

但是,我不知道您的实际数据模型,所以我只用WITH t AS ( <aggergate query here> ) SELECT t.Activities, tt.vcol, tt.val FROM t CROSS APPLY ( VALUES ('Bucket 0', [Bucket 0]), . . . ) tt (col, val); apply数据来做。