《Sql中的行列转》

—行转列
创建实例表:
create multiset volatile table vt1 (
cust_id varchar(10)
,class varchar(10)
,score deciaml(18,2)
)primary index(cust_id)
;

Insert into vt1 values (‘101’,’语文’,93);
Insert into vt1 values (‘101’,’数学,90);
Insert into vt1 values (‘101’,’英语’,89);

Insert into vt1 values (‘102,’语文’,83);
Insert into vt1 values (‘102’,’数学,91);
Insert into vt1 values (‘102’,’英语’,79);

Select
cust_id
, sum(case when class=‘语文’ then score else 0 end ) as “语文”
, sum(case when class=‘数学’ then score else 0 end )as “数学”
, sum(case when class=‘英语’ then score else 0 end ) as “英语”
from vt1
Group 1
;

—列转行操作

create multiset volatile table vt2 as (
Cust_id varchar(10)
,class_a decimal(10,2)
,class_b decimal(10,2)
,class_c decimal(10,2)
) primary index(cust_id) on commit preserve rows;

Insert into vt1 values (‘101,90,87,80);
Insert into vt1 values (‘102,83,91,90);

Select cuse_id , ’语文’,class_a from vt2 union
Select cuse_id , ’’数学, class_b from vt2 union
Select cuse_id , ’’英语, class_c from vt2

;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值