Assume we have the following table:
ID Type Amount
0001 A 10
0001 B 20
0001 C 30
0002 A 15
0002 C 20
0003 B 40
...
The desired result will be:
ID Type A Type B Type C
0001 10 20 30
I can do the following in order to combine multiple rows into one row.
SELECT ID, A, B, C
FROM MyTable
PIVOT
(
SUM(amount)
FOR Type IN (A, B, C)
) AS P
However, my table is more complicated that the previous one. Assume my table is the following:
ID Type Total_Amount Average_Amount
0001 A 10 0.5
0001 B 20 0.7
0001 C 30 0.9
So my question is how to produce the following results?
ID TypeA_total TypeB_total TypeC_total TypeA_avg TypeB_avg TypeC_avg
0001 10 20 30 0.5 0.7 0.9
Update:
The number of types is fixed. It means there are only Type A, B and C. Some ids may only have only 1 type. Some may have all 3 types. Some others may have 2 types.
Total_amount and Average_amount are given in the original table. I don't need to do calculation.
Here is the link that you can play with the table: http://sqlfiddle.com/#!3/4e6835