stdId StdName Subname SubjectMark
---------------------------------------------
1 alex english 50
2 anto english 60
2 anto hindhi 60
2 anto science 30
2 anto math 20
3 abru math 70
3 abru hindhi 60
3 abru english 50
我有一张如上所示的表格。我想写一个查询来获取值,如下所示
student english hindhi science math
----------------------------------------------
alex 50
anto 60 60 30
abru 50 70 70
请帮帮我.. 提前谢谢
答案 0 :(得分:0)
您没有指定正在使用的数据库,但是您应该能够在任何数据库中使用带有CASE表达式的聚合函数来将数据行转换为列。这个过程称为PIVOT:
select stdname,
max(case when subname = 'english' then subjectmark end) english,
max(case when subname = 'hindi' then subjectmark end) hindi,
max(case when subname = 'science' then subjectmark end) science,
max(case when subname = 'math' then subjectmark end) math
from yourtable
group by stdname;