表:
姓名 语文 数学 英语
张三 75 90 85
李四 80 85 85
查询结果
姓名 成绩 科目
张三 90 数学
李四 85 数学,英语
select * into CJ
from(
select '张三' as 姓名,75 as 语文,90 as 数学,85 as 英语
union
select '李四' as 姓名,80 as 语文,85 as 数学,85 as 英语
) a
----------------
select 姓名,max(成绩) as 成绩,
(select case when 语文=MAX(成绩) then '语文,' else '' end+
case when 数学=MAX(成绩) then '数学,' else '' end+
case when 英语=MAX(成绩) then '英语' else '' end
from CJ ab where ab.姓名=a.姓名) as 科目
from
(
select 姓名,语文 as 成绩,'语文' 科目
from CJ
union all
select 姓名,数学 as 成绩,'数学' 科目
from CJ
union all
select 姓名,英语 as 成绩,'英语' 科目
from CJ
) a group by a.姓名
from(
select '张三' as 姓名,75 as 语文,90 as 数学,85 as 英语
union
select '李四' as 姓名,80 as 语文,85 as 数学,85 as 英语
) a
----------------
select 姓名,max(成绩) as 成绩,
(select case when 语文=MAX(成绩) then '语文,' else '' end+
case when 数学=MAX(成绩) then '数学,' else '' end+
case when 英语=MAX(成绩) then '英语' else '' end
from CJ ab where ab.姓名=a.姓名) as 科目
from
(
select 姓名,语文 as 成绩,'语文' 科目
from CJ
union all
select 姓名,数学 as 成绩,'数学' 科目
from CJ
union all
select 姓名,英语 as 成绩,'英语' 科目
from CJ
) a group by a.姓名