I am trying to figure out what would be the best way in SQL or SSIS to pivot my results. I am pretty new to pivoting data and need some direction.
Here are my query results:
SELECT [ID]
,[Medication]
,[Route]
,[Day]
,Sum(Cast(Dose as float) as TotalDose
,Medication + '_' + Route + '_' + Cast(Day as varchar(10)) as PivotKey
FROM Table1
group by ID, Medication, Route, Day
ID Medication Route Day TotalDose PivotKey
1 Tylenol IV 1 1500 Tylenol_IV_1
1 Tylenol PO 1 1300 Tylenol_PO_1
1 Tylenol PO 2 1950 Tylenol_PO_2
2 Aspirin PO 1 1000 Aspirin_PO_1
2 Aspirin PO 2 500 Aspirin_PO_2
I have an existing table that stores these PivotKeys as columns, or there is always an option I guess to create a new table based on the data in PivotKey. I am trying to make the results look as follows:
ID Tylenol_IV_1 Tylenol_IV_2 Tylenol_PO_1 Tylenol_PO_2 Aspirin_PO_1 Aspirin_PO_2
----------
1 1500 0 1300 1950 0 0
2 0 0 0 0 1000 500
Thanks for the suggestions!