I have one table named grades like this:
+--------+--------+-----------+
| userid | itemid | finalgrade|
+--------+--------+-----------+
| 1 | 7 | 100 |
| 1 | 8 | 89 |
| 2 | 7 | 87 |
| 2 | 8 | 17 |
| 3 | 7 | 87 |
| 3 | 8 | 38 |
+--------+--------+-----------+
And I want to create a pivot query that changes me the table like this:
+--------+---+---+
| userid | 7 | 8 |
+--------+---+---+
| 1 |100| 89|
| 2 |87 | 17|
| 3 |87 | 38|
+--------+---+---+
I have one solution, using defined itemid like this one:
select *,
truncate (sum(finalgrade*(1-abs(sign(itemid-7)))),0) as 7,
truncate (sum(finalgrade*(1-abs(sign(itemid-8)))),0) as 8,
FROM grades
group by userid"
The problem is that I need the same query but with not defined itemid, dynamically creating columns.