行列转换
Posted JeanWan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了行列转换相关的知识,希望对你有一定的参考价值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
--drop table RowToCol
create
table
RowToCol
(
ID
int
,
Code
varchar
(10),
Value
int
)
Go
insert
RowToCol
select
1,
'Item1'
,1000
union
all
select
1,
'Item2'
,1000
union
all
select
1,
'Item3'
,500
union
all
select
2,
'Item1'
,2000
union
all
select
2,
'Item2'
,0
union
all
select
3,
'Item1'
,1000
union
all
select
3,
'Item3'
,500
GO
select
*
from
RowToCol
|
ID | Item1 | Item2 | Item3 |
1 | 1000 | 1000 | 500 |
2 | 2000 | 0 | 0 |
3 | 1000 | 0 | 500 |
1 2 3 4 5 6 7 8 9 10 11 |
select
ID,
sum
(
case
Code
when
'Item1'
then
Value
else
0
end
)
as
Item1,
sum
(
case
Code
when
'Item2'
then
Value
else
0
end
)
as
Item2,
Pandas行列转换的4大技巧
|