id为编号,a是选择题的答案,a的值在1-4之间select id,a from tableid a1 1
2 3
3 2a=1时 得25分
a=2时 得20分
a=3时 得15分
a=4时 得10分能否直接得出以下列表信息,并且计算出他们的总分id a fenshu1 1 25
2 3 15
3 2 20总分 25+15+20=60
2 3
3 2a=1时 得25分
a=2时 得20分
a=3时 得15分
a=4时 得10分能否直接得出以下列表信息,并且计算出他们的总分id a fenshu1 1 25
2 3 15
3 2 20总分 25+15+20=60
id a 1 1
2 3
3 2
----------------select id,a, fenshu=sum(case when a=1 then 25
when a=2 then 15
when a=3 then 20
else 10
end)
from t
group by id,a
when 2 then 15
when 3 then 20
else 0 end
from table
insert @t select 1,1
insert @t select 2,3
insert @t select 3,2
select id,sum(case a when 1 then 25
when 2 then 15
when 3 then 20
else 0 end)
from @t
group by id
with rollup/*id
----------- -----------
1 25
2 20
3 15
NULL 60(所影响的行数为 4 行)
*/
insert @t select 1,1
insert @t select 2,3
insert @t select 3,2
select id = case when GROUPING(id) = 1 then '总分'
else left(id,10) end,
fenshu
= sum(case a when 1 then 25
when 2 then 15
when 3 then 20
else 0 end)
from @t
group by id
with rollup/*
id fenshu
-------------------- -----------
1 25
2 20
3 15
总分 60
(所影响的行数为 4 行)
*/
无枪狙击手