Whilst trying to pivot a sql table I came across this post Here . By using this method I have created a query. However i have now realised that it of course aggregates the results with the MAX function. However I need the Colum to pivot but for all occurrences to be show. Code taken from above post.
SELECT dy,
MAX(CASE WHEN period = 1 THEN subj ELSE NULL END) AS P1,
MAX(CASE WHEN period = 2 THEN subj ELSE NULL END) AS P2,
FROM Classes
GROUP BY dy
So in essence I wish to use this but without the max function ? Any Ideas?
Edit Example data
Day Period Subject
Mon 1 Ch
Mon 2 Ph
Tue 1 Ph
Tue 2 Ele
Mon 1 Ch
Mon 2 Ph
Tue 1 Ph
Tue 2 Ele
example output
Day P1 P2
Mon Ch Ph
Mon Ch Ph
Tue Ph Ele
Tue Ph Ele
so basicly if the data is entered twice it appears twice...
Edit actual sql..
SELECT other
MAX(CASE WHEN period = 1 THEN table2.subj ELSE NULL END) AS P1,
MAX(CASE WHEN period = 2 THEN table2.subj ELSE NULL END) AS P2
FROM table1
left join table2 on table2.ID = subject
GROUP BY other
Example data
Table1
Dy Period Subject other
Mon 1 1 1
Mon 2 2 1
Tue 1 3 2
Tue 2 4 2
Mon 1 5 3
Mon 2 6 3
Tue 1 7 4
Tue 2 8 4
table2
ID Subj
1 ch
2 ph
3 ph
4 ele
5 ch
6 ph
7 ph
8 Ele
example output
Day P1 P2 other
Mon Ch Ph 1
Mon Ch Ph 3
Tue Ph Ele 2
Tue Ph Ele 4