У меня есть столбцы агрегированных данных, и я хочу PIVOT их в мой столбец 'TYPE'.
Мои данные выглядят так
ID Country TYPE Approved Dispursed Payments Amend Deob
---------- ------- ---------- -------- --------- -------- ----- ----
40002-0086 US Republic 79802 66163.05 0 0 0
40022-0002 Russia Sultanate 456435 0 3000 9789 578
40002-0002 Nkorea Dictatorship 50000 50000 0 0 789
40001-0001 Malta Democracy 200000 0 0 456 0
40016-0013 UAE Monarchy 65456 559542 0 0 0
40001-0002 Egypt Federal 250000 250000 0 0 56
40002-0012 Canada Parliamentary 7898 2000 0 789 0
И я пытаюсь сделать так, чтобы они выглядели какэто
ID Country Republic Approved Republic Disbursed Republic Repaid Sultanate Approved Sultanate Disbursed Federal Approved Federal Disbursed Monarchy Payments
44408 UAE 52,624,624.00 854,046.73 678,678.00 684,829.00 684,751.98 - - -
41898 Canada 23,423,423.00 3,454,345.00 58,810.00 783,766.15 783,766.15 453,654,423.00 8,368,354.00 386,836.00
Я пытаюсь использовать оператор PIVOT
Это то, что я пробовал до сих пор
SELECT 'ID' AS ID,
[Republic Approved],[Republic Disbursed],[Republic Repaid],[Sultanate Approved],[Sultanate Disbursed],[Federal Approved],[Federal Disbursed],[Monarchy Payments]
FROM
(SELECT ID, Country
FROM Table.ID) AS SourceTable
PIVOT
(
[TYPE]
FOR ID, Country IN ([Republic Approved],[Republic Disbursed],[Republic Repaid],[Sultanate Approved],[Sultanate Disbursed],[Federal Approved],[Federal Disbursed],[Monarchy Payments]
)
) AS PivotTable;