sql纵向表转成横向表
declare @tab table(Class varchar(20),Student varchar(20),Course varchar(50),Grades decimal(7,2));
insert into @tab(Class,Student,Course,Grades) values('A班','张三','语文',60);
insert into @tab(Class,Student,Course,Grades) values('A班','张三','数学',70);
insert into @tab(Class,Student,Course,Grades) values('A班','张三','英语',80);
insert into @tab(Class,Student,Course,Grades) values('A班','李四','语文',30);
insert into @tab(Class,Student,Course,Grades) values('A班','李四','数学',40);
insert into @tab(Class,Student,Course,Grades) values('A班','李四','英语',50);
insert into @tab(Class,Student,Course,Grades) values('B班','王五','语文',65);
insert into @tab(Class,Student,Course,Grades) values('B班','王五','数学',75);
insert into @tab(Class,Student,Course,Grades) values('B班','王五','英语',85);
insert into @tab(Class,Student,Course,Grades) values('B班','赵六','语文',35);
insert into @tab(Class,Student,Course,Grades) values('B班','赵六','数学',45);
insert into @tab(Class,Student,Course,Grades) values('B班','赵六','英语',55);
select * from @tab
select
(case when Grouping(Class)=1 then '总平均' when Grouping(Student)=1 then '' else Class end ) as Class
,(case when Grouping(Class)=1 then '' when Grouping(Student)=1 then '平均' else Student end) as Student
,cast(avg(语文) as decimal(7,2)) as 语文
,cast(avg(数学) as decimal(7,2)) as 数学
,cast(avg(英语) as decimal(7,2)) as 英语
,cast(avg(总分) as decimal(7,2)) as 总分
from (
select Class,Student
,(select isnull(sum(Grades),0) from @tab where class="t".Class and Student=t.Student and Course='语文') as '语文'
,(select isnull(sum(Grades),0) from @tab where class="t".Class and Student=t.Student and Course='数学') as '数学'
,(select isnull(sum(Grades),0) from @tab where class="t".Class and Student=t.Student and Course='英语') as '英语'
,(select isnull(sum(Grades),0) from @tab where class="t".Class and Student=t.Student) as '总分'
from @tab as t
group by Class,Student
) as tempTab
group by Class,Student,语文,数学,英语,总分 with rollup
having Grouping(语文)=1
and Grouping(数学)=1
and Grouping(英语)=1
SQL语句之普通行列转换
假设有张学生成绩表(Result)如下
Name Subject Result
张三 语文 73
张三 数学 83
张三
物理 93
李四 语文 74
李四 数学 84
李四 物理 94
建表:
create TABLE TABLE1 (Name varchar(50),Subject varchar(50),Result int)
INSERT INTO TABLE1 VALUES('张三','语文','73')
INSERT INTO TABLE1
VALUES('张三','数学','83')
INSERT INTO TABLE1 VALUES('张三','物理','93')
INSERT
INTO TABLE1 VALUES('李四','语文','74')
INSERT INTO TABLE1
VALUES('李四','数学','84')
INSERT INTO TABLE1 VALUES('李四','物理','94')
想变成
姓名 语文 数学 物理
张三 73 83 93
李四 74 84 94
declare @sql varchar(4000)
set @sql = 'select Name as ' + '姓名'
select
@sql = @sql + ' , sum(case Subject when '''+Subject+''' then Result end)
['+Subject+']'
from (select distinct Subject from TABLE1) as a
set @sql =
@sql + ' from TABLE1 group by name'
exec(@sql)
如果上述两表互相换一下:即
表名(cj)
姓名 语文 数学 物理
张三 73 83 93
李四 74 84 94
想变成
Name Subject Result
张三 语文 73
张三 数学 83
张三 物理 93
李四 语文 74
李四
数学 84
李四 物理 94
select 姓名 as Name,'语文' as Subject,语文 as Result from CJ union
select 姓名 as
Name,'数学' as Subject,数学 as Result from CJ union
select 姓名 as Name,'物理' as
Subject,物理 as Result from CJ
order by 姓名 desc
作者:阿笨
【官方QQ一群:跟着阿笨一起玩NET(已满)】:422315558
【官方QQ二群:跟着阿笨一起玩C#(已满)】:574187616
【官方QQ三群:跟着阿笨一起玩ASP.NET(已满)】:967920586
【官方QQ四群:Asp.Net Core跨平台技术开发(可加入)】:829227829
【官方QQ五群:.NET Core跨平台开发技术(可加入)】:647639415
【网易云课堂】:https://study.163.com/provider/2544628/index.htm?share=2&shareId=2544628
【腾讯课堂】:https://abennet.ke.qq.com
【51CTO学院】:https://edu.51cto.com/sd/66c64
【微信公众号】:微信搜索:跟着阿笨一起玩NET