Let's assume we have a table like:
| student_id | class_cd | order_cd |
| ---------- | ---------- | -------- |
| KG112 | TX113 | 1 |
| KG112 | AKI22 | 2 |
| KG112 | KK331 | 2 |
| KG112 | GMT18 | 2 |
| KG112 | ART12 | 2 |
| AO332 | KSD12 | 2 |
| AO332 | MCM15 | 1 |
| AO332 | KK331 | 2 |
| TL531 | CALX1 | 1 |
| TL531 | ALG15 | 2 |
| JP885 | ART12 | 1 |
| JP885 | AKI22 | 2 |
As shown from the table above, we assume that each student id can have 1 primary class and 4 secondary classes at once, for a total of 5 classes at once. How can I make a SELECT statement to show a result such that it looks like this:
| student_id | pri_class | sec_class1 | sec_class2 | sec_class3 | sec_class4 |
| ---------- | --------- | ---------- | ---------- | ---------- | ---------- |
| KG112 | TX113 | AKI22 | KK331 | GMT18 | ART12 |
| AO332 | MCM15 | KSD12 | KK331 | | |
| TL531 | CALX1 | ALG15 | | | |
| JP885 | ART12 | AKI22 | | | |
As you can see, each row pertains to that students classes where if they do not have up to 5 classes, then to leave the remainder blank. The order does not matter as to which classes show after the primary, only the primary, indicated by 1
has to be under pri_class
and the secondary, indicated by 2
can be randomly placed after the primary.
My attempt: literally been at this for 2 hours and have made 0 progress.