2016-12-20 6 views
0

에 행 그룹을 전치하는 방법 나는 다음과 같은 테이블 구조가 있습니다

NAME SUBJECT LEVEL  RESULT 
Smith maths beginner C 
Miller maths pro  B 
Prince maths beginner F 
Smith physics pro  B 
Miller physics pro  B 
Prince physics beginner E 

을 그리고 결과는 같을 것이다 그래서 나는, 이조의 어떤 종류를 원하는 :

NAME LEVEL_maths RESULT_maths LEVEL_physics RESULT_physics 
Smith beginner  C    pro    B 
Miller pro   B    pro    B 
... 

하나를 어떻게 이것이 (Postgre) SQL로 할 수 있겠는가? 어떤 힌트를 주셔서 감사합니다.

답변

0

이 시도 :

select name, 
max(case when subject='maths' then level else end)level_maths, 
max(case when subject='maths' then RESULT else end)RESULT_maths, 
max(case when subject='physics' then level else end)level_physics, 
max(case when subject='physics' then RESULT else end)RESULT_physics 
from test 
group by name; 
+0

고마워 Bhavesh, 즉 빨리이었다. 그리고 정확히 내가 무엇을 찾고 있었는지. – Prefect73