如何将列转换为行?
How I can convert those colum to row?
转换后应该是
month 1 2 3 4 5 6 7 8 9 10 11 12
planqty 0 0 6230 0 0 0 0 0 0 0 0 0
actqty 0 0 2631 0 0 0 0 0 0 0 0 0
如果您能提供帮助,我将不胜感激。
您需要APPLY
(即CROSS APPLY
)以便将列转换为行。
select
qtynames as Month,
max(case when dt = '01' then qty end) [01],
max(case when dt = '02' then qty end) [02],
max(case when dt = '03' then qty end) [03],...
from table t cross apply (
values ('month', dt, 'planqty', planqty), ('month', dt, 'actqty', actqty)
)a(mnames, dates, qtynames, qty)
group by qtynames
转换后应该是
month 1 2 3 4 5 6 7 8 9 10 11 12
planqty 0 0 6230 0 0 0 0 0 0 0 0 0
actqty 0 0 2631 0 0 0 0 0 0 0 0 0
如果您能提供帮助,我将不胜感激。
您需要APPLY
(即CROSS APPLY
)以便将列转换为行。
select
qtynames as Month,
max(case when dt = '01' then qty end) [01],
max(case when dt = '02' then qty end) [02],
max(case when dt = '03' then qty end) [03],...
from table t cross apply (
values ('month', dt, 'planqty', planqty), ('month', dt, 'actqty', actqty)
)a(mnames, dates, qtynames, qty)
group by qtynames