SQL 如何查询时 竖着的数据 横着显示 下载本文

SQL 如何查询时 竖着的数据 横着显示??

姓名 科目 分数

王明 数学 100

王明 语文 100

王明 英语 100

显示成

姓名 数学 语文 英语

王明 100 100 100

提问者采纳

select name,

sum(case when subject='数学' then score else 0 end) as '数学', sum(case when subject='语文' then score else 0 end) as '语文', sum(case when subject='英语' then score else 0 end) as '英语' from table group by name

最好有学号区分,如果重名就不好弄了

我写的查询就餐次数SQL:

select a.dpt_nm,a.emp_no, a.emp_nm, brecnt, luncnt, supcnt, b.emp_no, b.chn_nm ,c.dpt_cd,c.dpt_nm_ch

from(

select a.dpt_nm,a.emp_no, a.emp_nm,

SUM(CASE WHEN a.csm_my='3.00' then 1 else 0 end) as brecnt, SUM(CASE WHEN a.csm_my='6.00' then 1 else 0 end) as luncnt, SUM(CASE WHEN a.csm_my='5.00' then 1 else 0 end) as supcnt from ATT_REPAST_i A

where 1=1

and a.csm_dt >='2016-03-01' and a.csm_dt<'2016-03-03' group by a.dpt_nm,a.emp_no, a.emp_nm) a

left outer join per_employee_i B ON a.EMP_NO=b.EMP_NO

left outer join per_department_i c on b.departmentid=c.departmentid where 1=1

order by c.dpt_cd, b.emp_no;