I have a table structure somewhat like the following:
CREATE TABLE #temp
(
period_id INTEGER ,
lease_id INTEGER ,
charge_id VARCHAR(20) ,
charge_amount MONEY
)
INSERT INTO #temp
( period_id ,
lease_id ,
charge_id ,
charge_amount
)
VALUES ( 100 , -- period_id - integer
2000 , -- lease_id - integer
'300' , -- charge_id - varchar(20)
12345 -- charge_amount - money
) ,
( 101 , -- period_id - integer
2000 , -- lease_id - integer
'300' , -- charge_id - varchar(20)
678910 -- charge_amount - money
) ,
( 101 , -- period_id - integer
2002 , -- lease_id - integer
'300' , -- charge_id - varchar(20)
78950 -- charge_amount - money
) ,
( 101 , -- period_id - integer
2002 , -- lease_id - integer
'310' , -- charge_id - varchar(20)
9002 -- charge_amount - money
)
I want an output that looks like the following:
I'm confident I could get there by creating a series of cte's and constantly referring back to the source table however I don't think that's a great solution here as I have a lot of these charge codes and new ones are expected to be added in the future (and having to update the query each time this happens would be terrible practice). I've looked into the pivot function but I don't understand how to use it, I'd like to understand how to apply it in this scenario so I can add this into my arsenal.