I am trying to write a query that pivots multiple rows and columns into a single easy to read columns. This is what the source data looks like.
Time | Source | Temp1 | Temp 2 | Temp 3 | Temp 4 |
---|---|---|---|---|---|
1 | 1 | 10 | 11 | 12 | 13 |
1 | 2 | 20 | 21 | 22 | 23 |
1 | 3 | 30 | 31 | 32 | 33 |
1 | 4 | 40 | 41 | 42 | 43 |
This is what I want
Time | Src1_Temp1 | Src1_Temp 2 | Src1_Temp 3 | Src1_Temp 4 | Src2_Temp1 | Src2_Temp 2 | Src2_Temp 3 | Src2_Temp 4 |
---|---|---|---|---|---|---|---|---|
1 | 10 | 11 | 12 | 13 | 20 | 21 | 21 | 23 |
I am attempting to use a Pivot in SQL, however, I am having a hard time defining multiple aggregates within the pivot
Select Time,
[1],
[2],
[3],
[4],
FROM
(
SELECT Source
, Time
,Temp1
,Temp2
,Temp2
,Temp3
FROM MySourceTable
group by Time, Source, Temp1, Temp2, Temp3, Temp4
) as SourceTable
PIVOT
(
MAX(Temp1)
FOR Source in ([1] Src1, [2] Src2, [3] Src3, [4] Src4)
) as Pivot
The issue I have is, I cannot select Max(Temp1), Max(Temp2), Max(Temp3), Max(Temp4) in the PIVOT.
Any help would be greatly appreciated