Id | Name | Class | Pay_date(수강연월) | Reg_date(최초학원등록연월) |
001 | 홍길동 | 국어 | 201601 | 201510 |
002 | 강감찬 | 영어 | 201601 | 201511 |
003 | 이순신 | 수학 | 201601 | 201512 |
003 | 이순신 | 영어 | 201601 | 201512 |
002 | 강감찬 | 수학 | 201602 | 201511 |
003 | 이순신 | 국어 | 201602 | 201512 |
004 | 김두한 | 국어 | 201602 | 201602 |
001 | 홍길동 | 영어 | 201603 | 201510 |
002 | 강감찬 | 과학 | 201603 | 201511 |
학생들이 수강한 위의 데이터를 가지고 아래와 같이 만들고 싶습니다.
Id | Name | 국어 | 영어 | 수학 | 과학 | 사회 | 수강연월 |
001 | 홍길동 | 1 | 0 | 0 | 0 | 0 | 201601 |
002 | 강감찬 | 0 | 1 | 0 | 0 | 0 | 201601 |
003 | 이순신 | 0 | 1 | 1 | 0 | 0 | 201601 |
004 | 김두한 | 1 | 0 | 0 | 0 | 0 | 201602 |
001 | 홍길동 | 0 | 1 | 0 | 0 | 0 | 201603 |
002 | 강감찬 | 0 | 0 | 0 | 1 | 0 | 201603 |
방법을 알려주시면 정말 감사하겠습니다-!
id(pk) 가 중복값이 있네요 ...
어떤기준으로 그룹바이 해야할지 모호하네요 ...
결과테이블이 원하는결과가맞나요? 제가짠쿼리는 님이올린결과랑틀리게나오네요 ...
기준을 정확하게정해주세요 ...
WITH T AS ( select '001' as id, '홍길동' as name, '국어' as class, '201601' as pay_date, '201510' as reg_date from dual union all select '002', '강감찬', '영어', '201601', '201511' from dual union all select '003', '이순신', '수학', '201601', '201512' from dual union all select '003', '이순신', '영어', '201601', '201512' from dual union all select '002', '강감찬', '수학', '201602', '201511' from dual union all select '003', '이순신', '국어', '201602', '201512' from dual union all select '004', '김두한', '국어', '201602', '201602' from dual union all select '001', '홍길동', '영어', '201603', '201510' from dual union all select '002', '강감찬', '과학', '201603', '201511' from dual ) select t.pay_date ,t.id ,t.name ,sum(decode(t.class,'국어',1,0)) as 국어 ,sum(decode(t.class,'영어',1,0)) as 영어 ,sum(decode(t.class,'수학',1,0)) as 수학 ,sum(decode(t.class,'과학',1,0)) as 과학 ,sum(decode(t.class,'사회',1,0)) as 사회 from t group by t.pay_date ,t.id ,t.name order by t.pay_date ,t.id ,t.name