有这样一个表
姓名 科目 成绩
张三 语文 80
张三 数学 50
张三 英语 95
张三 物理 75
李四 语文 55
李四 数学 90
李四 英语 65
李四 物理 95
要查询后的结果变为这样排列
姓名 文科成绩 理科成绩
张三 175 125
李四 120 185
这个SQL语句怎么写。
如果这样排列又应怎么写
姓名 语文 数学 英语 物理
大家帮忙啊
姓名 科目 成绩
张三 语文 80
张三 数学 50
张三 英语 95
张三 物理 75
李四 语文 55
李四 数学 90
李四 英语 65
李四 物理 95
要查询后的结果变为这样排列
姓名 文科成绩 理科成绩
张三 175 125
李四 120 185
这个SQL语句怎么写。
如果这样排列又应怎么写
姓名 语文 数学 英语 物理
大家帮忙啊
或者唯一 distinct
chengji A where A.Name = B.Name and A.subject in ('语文','英语')) as 文科成绩,
(select sum(result) from chengji C Where C.name = B.name and C.subject in ('数学','物理')) as 理科成绩
from chengji B group by name
这个好像有个povit交叉表语法
chengji A where A.Name = B.Name and A.subject in ('语文','英语')) as 文科成绩,
(select sum(result) from chengji C Where C.name = B.name and C.subject in ('数学','物理')) as 理科成绩
from chengji B group by name这个a.name与b.name a.subject是什么啊
transform sum(成绩) select 姓名 from 表名 group by 姓名 pivot 科目 in ('语文','数学','英语','物理')
select name ,(select sum(result) from cj b where b.name=a.name and b.subject in ('语文','英语'))as 文科成绩,(select sum(result)from cj c where c.name=a.name and c.subject in ('物理','数学')) as 理科成绩 from cj a groub by a.name
the secend question:
select name ,(select result where b.name=a.name b.subject='语文' from cj b) as 语文,(select result where c.name=a.name and c.subject='外语' from cj c),(select result where d.name=a.name and d.subject='数学' from cj d),select result where e.name=a.name and e.subject='物理' from cj e) from cj a group by name