兴国资源网 Design By www.nnzcdc.com
效果如图所示:
一句Sql把纵向表转为横向表,并分别分组求平均和总平均值
测试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
兴国资源网 Design By www.nnzcdc.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
兴国资源网 Design By www.nnzcdc.com

P70系列延期,华为新旗舰将在下月发布

3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。

而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?

根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。