一句Sql把纵向表转为横向表,并分别分组求平均和总平均值

所属分类: 数据库 / MsSql 阅读数: 1713
收藏 0 赞 0 分享
效果如图所示:

测试sql语句如下:
复制代码 代码如下:

declare @tab table(Class varchar(20),Student varchar(20),Course varchar(50),Quantity decimal(7,2));
insert into @tab(Class,Student,Course,Quantity) values('A班','张三','语文',60);
insert into @tab(Class,Student,Course,Quantity) values('A班','张三','数学',70);
insert into @tab(Class,Student,Course,Quantity) values('A班','张三','英语',80);
insert into @tab(Class,Student,Course,Quantity) values('A班','李四','语文',30);
insert into @tab(Class,Student,Course,Quantity) values('A班','李四','数学',40);
insert into @tab(Class,Student,Course,Quantity) values('A班','李四','英语',50);

insert into @tab(Class,Student,Course,Quantity) values('B班','王五','语文',65);
insert into @tab(Class,Student,Course,Quantity) values('B班','王五','数学',75);
insert into @tab(Class,Student,Course,Quantity) values('B班','王五','英语',85);
insert into @tab(Class,Student,Course,Quantity) values('B班','赵六','语文',35);
insert into @tab(Class,Student,Course,Quantity) values('B班','赵六','数学',45);
insert into @tab(Class,Student,Course,Quantity) 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
,avg(语文) as 语文
,avg(数学) as 数学
,avg(英语) as 英语
,avg(总分) as 总分
from (
select Class,Student
,(select isnull(sum(Quantity),0) from @tab where Class=t.Class and Student=t.Student and Course='语文') as '语文'
,(select isnull(sum(Quantity),0) from @tab where Class=t.Class and Student=t.Student and Course='数学') as '数学'
,(select isnull(sum(Quantity),0) from @tab where Class=t.Class and Student=t.Student and Course='英语') as '英语'
,(select isnull(sum(Quantity),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
更多精彩内容其他人还在看

一个函数解决SQLServer中bigint 转 int带符号时报错问题

这篇文章主要介绍了解决SQLServer中bigint 转 int带符号时报错问题的函数,需要的朋友可以参考下
收藏 0 赞 0 分享

SQLServer恢复表级数据详解

这篇文章主要介绍了SQLServer中用于快速恢复表,而不是库,但是切记,防范总比亡羊补牢好,需要的朋友可以参考下
收藏 0 赞 0 分享

SQLSERVER 清除历史记录的方法

使用SQL Server登录使用数据库时,登录过的记录会出现在登录框中,下面是删除方法,需要的朋友可以参考下
收藏 0 赞 0 分享

浅析SQL server 临时表

这篇文章主要介绍了SQL server 临时表的创建查询以及使用过程中应注意的事项,需要的朋友可以参考下
收藏 0 赞 0 分享

MSSQL段落还原脚本,SQLSERVER段落脚本

“段落还原”(在 SQL Server 2005 中引入)允许分阶段还原和恢复包含多个文件组的数据库。段落还原包括从主文件组开始(有时也从一个或多个辅助文件组开始)的一系列还原序列。
收藏 0 赞 0 分享

SQL SERVER中关于exists 和 in的简单分析

这篇文章主要介绍了SQL SERVER中关于exists 和 in的简单分析,需要的朋友可以参考下
收藏 0 赞 0 分享

Transactional replication(事务复制)详解之如何跳过一个事务

事务复制由 SQL Server 快照代理、日志读取器代理和分发代理实现。 快照代理准备快照文件(其中包含了已发布表和数据库对象的架构和数据),然后将这些文件存储在快照文件夹中,并在分发服务器中的分发数据库中记录同步作业。
收藏 0 赞 0 分享

SQL语句中含有乘号报错的处理办法

这篇文章主要介绍了SQL语句中含有乘号报错的处理办法,需要的朋友可以参考下
收藏 0 赞 0 分享

SQL SERVER先判断视图是否存在然后再创建视图的语句

SQL SERVER中先判断视图是否存在,使用IF NOT EXISTS,然后再创建视图,使用create view,整个过程如下
收藏 0 赞 0 分享

sql中循环处理当前行数据和上一行数据相加减

曾经,sql中循环处理当前行数据和上一行数据浪费了我不少时间,学会后才发现如此容易,其实学问就是如此,难者不会,会者不难。
收藏 0 赞 0 分享
查看更多