表如下:
ID NO Name Course Score
1 001 赵 语文 80
2 001 赵 数学 40
3 001 赵 英语 60
4 002 李 语文 50
5 002 李 数学 30
6 003 唐 语文 20
Sql语句执行结果如下:
NO Name AllCouse 总分
001 赵 语文,数学,英语 180
002 李 语文,数学 80
003 唐 语文 20
ID NO Name Course Score
1 001 赵 语文 80
2 001 赵 数学 40
3 001 赵 英语 60
4 002 李 语文 50
5 002 李 数学 30
6 003 唐 语文 20
Sql语句执行结果如下:
NO Name AllCouse 总分
001 赵 语文,数学,英语 180
002 李 语文,数学 80
003 唐 语文 20
(select no,name, case when 语文<>0 then '语文' end yw,case when 数学<>0 then ',数学' end xx,case when 英语<>0 then ',英语' end yy,
"语文","数学","英语" from
(select no,name ,nvl(sum("语文"),0) "语文",nvl(sum("数学"),0) "数学",nvl(sum("英语"),0) "英语"
from (select no,name,
case when Course='语文' then nvl(SUM(score),0) end "语文",
case when Course='数学' then nvl(SUM(score),0) end "数学",
case when Course='英语' then nvl(SUM(score),0) end "英语"/*,case when Course='语文' then nvl(SUM(score),0) end "语文s",
case when Course='数学' then nvl(SUM(score),0) end "数学s",
case when Course='英语' then nvl(SUM(score),0) end "英语s"*/from lht_test
group by no,name,course) t
group by no,name) tt) ttt
(select no,name, case when 语文<>0 then '语文' end yw,case when 数学<>0 then ',数学' end xx,case when 英语<>0 then ',英语' end yy,
"语文","数学","英语" from
(select no,name ,nvl(sum("语文"),0) "语文",nvl(sum("数学"),0) "数学",nvl(sum("英语"),0) "英语"
from (select no,name,
case when Course='语文' then nvl(SUM(score),0) end "语文",
case when Course='数学' then nvl(SUM(score),0) end "数学",
case when Course='英语' then nvl(SUM(score),0) end "英语"from lht_test
group by no,name,course) t
group by no,name) tt) ttt
可是课程名是未知的,也就是说语文,数学,英语,之外还有可能有其它的,比如政治什么的。
我现在正想办法在SqlSever上定义一个函数。