sql语句列转行

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql语句列转行相关的知识,希望对你有一定的参考价值。

参考技术A 主要应用case语句来解决行转列的问题
行转列问题主要分为两类
1)简单的行转列问题:
示例表:
id
sid
course
result
1
2005001
语文
80.0
2
2005001
数学
90.0
3
2005001
英语
80.0
4
2005002
语文
56.0
5
2005002
数学
69.0
6
2005002
英语
89.0
执行
select
sid,语文=isnull(sum(case
course
when
'语文'
then
result
end),0),
数学=isnull(sum(case
course
when
'数学'
then
result
end),0),
英语=isnull(sum(case
course
when
'英语'
then
result
end),0)
from
result
group
by
sid
order
by
sid
得出结果
sid
语文
数学
英语
2005001
80.0
90.0
80.0
2005002
56.0
69.0
89.0
参考技术B 这个不是用sql解决,而是用脚本语言解决!或者sql存数据的时候把c_003
c_004
c_005
变成字段,再录数据。转换也是用脚本语言!

sql 行转列 列转行

1、行转列一般通过case when语句来实现。

create table studentscores(
    username varchar(20),
    subject varchar(30),
    score float
);
insert into studentscores select Nick,语文,80;
insert into studentscores select Nick,数学,90;
insert into studentscores select Nick,英语,70;
insert into studentscores select Nick,生物,85;
insert into studentscores select Kent,语文,80;
insert into studentscores select Kent,数学,90;
insert into studentscores select Kent,英语,70;
insert into studentscores select Kent,生物,85;

select username,
 max(case subject when 语文 then score else 0 end) as 语文,
 max(case subject when 数学 then score else 0 end) as 数学,
 max(case subject when 英语 then score else 0 end) as 英语,
 max(case subject when 生物 then score else 0 end) as 生物
from studentscores
GROUP BY username;

技术图片

 

create table inpours
(
    id int primary key AUTO_INCREMENT,
    username varchar(20),
    createtime datetime,
    paytype varchar(20),
    money decimal,
    issuccess bit
);

insert into inpours select 1,张三,2010-05-01,支付宝,50,1;
insert into inpours select 2,张三,2010-06-14,支付宝,50,1;
insert into inpours select 3,张三,2010-06-14,手机短信,100,1;
insert into inpours select 4,李四,2010-06-14,手机短信,100,1;
insert into inpours select 5,李四,2010-07-14,支付宝,100,1;
insert into inpours select 6,王五,2010-07-14,工商银行卡,100,1;
insert into inpours select 7,赵六,2010-07-14,建设银行卡,100,1;

select createtime,
 case paytype when 支付宝 then sum(money) else 0 end as 支付宝,
 case paytype when 手机短信 then sum(money) else 0 end as 手机短信,
 case paytype when 工商银行卡 then sum(money) else 0 end as 工商银行卡,
 case paytype when 建设银行卡 then sum(money) else 0 end as 建设银行卡
from inpours
group by createtime,paytype;

技术图片

2、列转行,主要是通过UNION ALL,MAX来实现

create table progrectdetail(
    progrectname varchar(20),
    overseasupply int,
    nativesupply int,
    southsupply int,
    northsupply int
);

insert into progrectdetail select A,100,200,50,50
union ALL
select B,200,300,150,150
union ALL
select C,159,400,20,320
union ALL
select D,250,30,15,15;

技术图片

select progrectname,OverseaSupply as supplier,
max(overseasupply) as supplynum
from progrectdetail
GROUP BY progrectname
union ALL
select progrectname,nativesupply as supplier,
max(nativesupply) as supplynum
from progrectdetail
GROUP BY progrectname
union ALL
select progrectname,southsupply as supplier,
max(southsupply) as supplynum
from progrectdetail
GROUP BY progrectname
union ALL
select progrectname,northsupply as supplier,
max(northsupply) as supplynum
from progrectdetail
GROUP BY progrectname;

技术图片

以上是关于sql语句列转行的主要内容,如果未能解决你的问题,请参考以下文章

Flink SQL 如何实现列转行 ?

重温SQL——行转列,列转行

sql 行转列 列转行

SQL Server 行转列,列转行

SQL Server FOR XML PATH 语句的应用---列转行

ORACLE 同表列转行