I know this has been asked many, many times before, but I struggle to find the solution to what I need.
Data:
Id FKId TypeId Score ScoreDate
1 317 1 90 2019-01-01
2 317 1 80 2019-01-02
3 317 2 65 2019-01-03
4 317 2 80 2019-01-04
5 318 1 82 2019-01-01
6 318 1 78 2019-01-02
7 318 2 55 2019-01-03
8 318 2 63 2019-01-04
Expected Result:
FkId A1 A2 B1 B2
317 90 80 65 80
318 82 78 55 63
I tried to use a pivot to get me data, by the bit that I stumble over is that you need to use an aggregate function and that result in the wrong result. Example of one of the attempts:
select *
from
(
select [FkId], [TypeId], [Score]
from #yt
) src
pivot
(
max([Score])
for [TypeId] in ([1], [2], [3])
) piv;
query result:
FkId 1 2 3
101 83 82 NULL
102 96 80 NULL
I understand that I will need to have more than one query to get to the expected result, but that is not my concern, for now I just want to transpose one type of score with each score into it's own column.
Any help will do.