PIVOT on Multiple Columns

前端 未结 3 618
南旧
南旧 2021-01-26 20:41

I have data like this:

Product Group     Product Level    Quatity Sold     Trend
==============================================================
Group 1                   


        
3条回答
  •  没有蜡笔的小新
    2021-01-26 21:11

    if you prefer to use pivot you can try this one:

    select productgroup, 
    coalesce(L1up,L1down,'') L1, case when L1up is not null then 'up' when L1down is not null then 'down' else '' end L1trend,
    coalesce(L2up,L2down,'') L2, case when L2up is not null then 'up' when L2down is not null then 'down' else '' end L2trend,
    coalesce(L3up,L3down,'') L3, case when L3up is not null then 'up' when L3down is not null then 'down' else '' end L3trend,
    coalesce(L4up,L4down,'') L4, case when L4up is not null then 'up' when L4down is not null then 'down' else '' end L4trend
    from
    (
    select productgroup, [L1up],[L2up],[L3up],[L4up],[L1down],[L2down],[L3down],[L4down] 
    from (select productgroup, productlevel+trend pt, quantity from mytable) t
    PIVOT (MAX(quantity)
    FOR pt IN([L1up],[L2up],[L3up],[L4up],[L1down],[L2down],[L3down],[L4down] )) as p
    ) t
    

提交回复
热议问题