with cte as (select *,case when c_name in ('语文','数学','英语') then '文科' else '理综' end as groupid from course ) select b.stu_id,a.groupid,sum(score) as sums from cte as a join score as b on a.c_id=b.c_id group by stu_id,groupid order by stu_id,groupid desc--结果 stu_id groupid sums ----------- ------- ---------------------- 1 文科 227 1 理综 176 2 文科 224 2 理综 178 3 文科 221 3 理综 194 4 文科 218 4 理综 172 5 文科 215 6 文科 212 6 理综 168
select s.stu_name, case c_id when 1 then '文' when 2 then '文' when 3 then '文' when 4 then '理' when 5 then '理' when 6 then '理' end, sum(sc.score) from Stus s left join Score sc on s.stu_id=sc.stu_id group by s.stu_name, case c_id when 1 then '文' when 2 then '文' when 3 then '文' when 4 then '理' when 5 then '理' when 6 then '理' end
这是什么版本的语法,能否再进行列转行呢,楼下编号5的理综应该是没有的,你的结果是对的 08就可以。行转列用个PIVOT 不就OK了吗 看你具体需要转换成啥格式。 alimake: group by stu_name 文科,理科分数行转列 没有分补空 说错了 对这种语法没研究 能不能把整个语句写出来看看
这是什么版本的语法,能否再进行列转行呢,楼下编号5的理综应该是没有的,你的结果是对的 08就可以。行转列用个PIVOT 不就OK了吗 看你具体需要转换成啥格式。 alimake: group by stu_name 文科,理科分数行转列 没有分补空 说错了 对这种语法没研究 能不能把整个语句写出来看看 with cte as (select *,case when c_name in ('语文','数学','英语') then '文科' else '理综' end as groupid from course ) select * from (select b.stu_id,a.groupid,sum(score) as sums from cte as a join score as b on a.c_id=b.c_id group by stu_id,groupid) as t pivot (sum(sums) for groupid in(文科,理综))as t--结果 stu_id 文科 理综 ----------- ---------------------- ---------------------- 1 227 176 2 224 178 3 221 194 4 218 172 5 215 NULL 6 212 168(6 行受影响)
with cte as ( select c.stu_name,b.c_name,score from Score a left join Course b on a .c_id=b.c_id left join Stus c on a.stu_id=c.stu_id ) select stu_name , sum(case when (c_name='语文' or c_name='数学' or c_name='英语') then score else 0 end ) as '文科', sum(case when (c_name='物理' or c_name='化学' or c_name='生物') then score else 0 end ) as '理综' from cte group by stu_name
select * from( select case when c.c_id in('1','2','3') then '文科' else '理科' end coure,a.stu_name,sum(isnull(score,0))score from Course as c left join Score as b on c.c_id=b.c_id left join Stus a on a.stu_id=b.stu_id group by c.c_name,a.stu_name,stu_name,case when c.c_id in('1','2','3') then '文科' else '理科' end ) as c pivot(max(score) for coure in([理科],[文科]))v
(select *,case when c_name in ('语文','数学','英语') then '文科' else '理综' end as groupid
from course )
select b.stu_id,a.groupid,sum(score) as sums from cte as a join score as b
on a.c_id=b.c_id
group by stu_id,groupid
order by stu_id,groupid desc--结果
stu_id groupid sums
----------- ------- ----------------------
1 文科 227
1 理综 176
2 文科 224
2 理综 178
3 文科 221
3 理综 194
4 文科 218
4 理综 172
5 文科 215
6 文科 212
6 理综 168
when 1 then '文'
when 2 then '文'
when 3 then '文'
when 4 then '理'
when 5 then '理'
when 6 then '理'
end,
sum(sc.score)
from Stus s left join Score sc on s.stu_id=sc.stu_id
group by s.stu_name,
case c_id
when 1 then '文'
when 2 then '文'
when 3 then '文'
when 4 then '理'
when 5 then '理'
when 6 then '理'
end
这是什么版本的语法,能否再进行列转行呢,楼下编号5的理综应该是没有的,你的结果是对的
08就可以。行转列用个PIVOT 不就OK了吗 看你具体需要转换成啥格式。
这是什么版本的语法,能否再进行列转行呢,楼下编号5的理综应该是没有的,你的结果是对的
08就可以。行转列用个PIVOT 不就OK了吗 看你具体需要转换成啥格式。
alimake: group by stu_name 文科,理科分数行转列 没有分补空 说错了 对这种语法没研究 能不能把整个语句写出来看看
这是什么版本的语法,能否再进行列转行呢,楼下编号5的理综应该是没有的,你的结果是对的
08就可以。行转列用个PIVOT 不就OK了吗 看你具体需要转换成啥格式。
alimake: group by stu_name 文科,理科分数行转列 没有分补空 说错了 对这种语法没研究 能不能把整个语句写出来看看
with cte as
(select *,case when c_name in ('语文','数学','英语') then '文科' else '理综' end as groupid
from course )
select * from (select b.stu_id,a.groupid,sum(score) as sums from cte as a join score as b
on a.c_id=b.c_id
group by stu_id,groupid) as t
pivot (sum(sums) for groupid in(文科,理综))as t--结果
stu_id 文科 理综
----------- ---------------------- ----------------------
1 227 176
2 224 178
3 221 194
4 218 172
5 215 NULL
6 212 168(6 行受影响)
(
select c.stu_name,b.c_name,score from Score a
left join Course b on a .c_id=b.c_id
left join Stus c on a.stu_id=c.stu_id )
select stu_name , sum(case when (c_name='语文' or c_name='数学' or c_name='英语') then score else 0 end ) as '文科',
sum(case when (c_name='物理' or c_name='化学' or c_name='生物') then score else 0 end ) as '理综'
from cte group by stu_name
-- stu_name 文科 理综
--八大婶 212 168
--李四 224 178
--七大姑 215 0
--王五 221 194
--张三 227 176
--赵六 218 172
写法很多
select case when c.c_id in('1','2','3') then '文科' else '理科' end coure,a.stu_name,sum(isnull(score,0))score from Course as c left join Score as b on c.c_id=b.c_id
left join Stus a on a.stu_id=b.stu_id group by c.c_name,a.stu_name,stu_name,case when c.c_id in('1','2','3') then '文科' else '理科' end
) as c pivot(max(score) for coure in([理科],[文科]))v