To do the pivot, just add another cte to the code @Russel Loski gave you, so
with cteAmount as (
SELECT ID, AmountType, Code, SUM(AMOUNT) as Amount
FROM #Temp
GROUP BY ID, AmountType, Code),
cteCode As
(SELECT
ID, AmountType
, STRING_AGG(Code,',') Within Group (Order By Code) as Type
, Sum(Amount) as Amount
From cteAmount
GROUP BY ID, AmountType)
Select ID,
IsNull(Max(Case When AmountType = 'CR' Then Amount End), 0) As CR,
IsNull(Max(Case When AmountType = 'DR' Then Amount End), 0) As DR,
IsNull(Max(Case When AmountType = 'CR' Then Type End), '') As CRCode,
IsNull(Max(Case When AmountType = 'DR' Then Type End), '') As DRCode
From cteCode
Group By ID
Order By ID;
Tom