>>>;with cte as ( >>>select P.AmendmentNumber, coalesce(P.TipId, A.TipId) as TipId, coalesce(P.Amount,0) - coalesce(A.Amount,0) as Amount, >>>coalesce(P.FundType, A.FundType) as FundType >>>from TIP_AmendmentProposedFunding P >>>full join TIP_AmendmentApprovedFunding A >>>on P.TipID = A.TipID and P.FundType = A.FundType >>>where P.AmendmentNumber = 2014.01 and A.AmendmentNumber = 2014.01 >>>) >>> >>>select * from CTE PIVOT (sum(Amount) for FundType IN ([ASTP], [RTA], [HURF26], [DIFO], [Local], [STP])) pvt >>>>>>This gets pretty close but the computation is off. here is some real data.
>; > ) >select *with t1 as ( >select coalesce(p.amendmentnumber, a.amendmentnumber) as AmendmentNumber, >d.ItemNumber, >coalesce(p.tipid, a.tipid) as TipID, >coalesce(p.fundtype, a.fundtype) as FundType, >sum(coalesce(p.AMOUNT,0)- coalesce(A.Amount,0)) as NET >from TIP_AmendmentProposedFunding P >full outer join TIP_AmendmentApprovedFunding A >on P.TipID = a.TipID and p.AmendmentNumber = a.AmendmentNumber and p.YEAR = a.year and p.FundType = a.fundtype >full join TIP_AmendmentDetails d >ON d.AmendmentNumber = p.amendmentnumber and d.TipID = p.tipid >where (coalesce(p.amendmentnumber, a.amendmentnumber)=2014.01 ) >group by coalesce(p.amendmentnumber, a.amendmentnumber),ItemNumber,coalesce(p.tipid, a.tipid),coalesce(p.fundtype, a.fundtype) >from t1 pivot (sum(NET) for FundType in ([ASTP], [RTA], [HURF26], [DIFO], [Local], [STP])) pvt >>but the dynamic part eludes me even after studying your (and others) samples. Can you describe the principle involved? What exactly is the approach? I can see that the dynamic string needs to be built with something like
SELECT distinct FundType FROM ....the result in the first part of the abovebut the samples all leave me befuddled.
declare @SQL varchar(max), @Columns VARCHAR(max) set @columns = stuff((select ', ' + quotename(FundType) FROM ( SELECT FundType from TIP_AmendmentProposedFunding P UNION SELECT FundType FROM TIP_AmendmentDetails) S ORDER BY FundType FOR XML PATH('').type).value('.', 'varchar(30)')), 1,2,'') -- list of new columns set @SQL = ';with cte as (select coalesce(p.amendmentnumber, a.amendmentnumber) as AmendmentNumber, d.ItemNumber, coalesce(p.tipid, a.tipid) as TipID, coalesce(p.fundtype, a.fundtype) as FundType, coaleasce(p.AMOUNT,0) - coalesce(A.Amount,0) as NET from (SELECT AmendementNumber, FundType, TipId, SUM(Amount) as Amount FROM TIP_AmendmentProposedFunding GROUP BY AmendementNumber, FundType, TipId ) P full outer join (SELECT AmendementNumber, FundType, TipId, SUM(Amount) as Amount FROM TIP_AmendmentApprovedFunding GROUP BY AmendementNumber, FundType, TipID) A on P.TipID = a.TipID and p.AmendmentNumber = a.AmendmentNumber and p.YEAR = a.year and p.FundType = a.fundtype full join TIP_AmendmentDetails d ON d.AmendmentNumber = p.amendmentnumber and d.TipID = p.tipid where (coalesce(p.amendmentnumber, a.amendmentnumber)=2014.01 ) -- do you need that number only - if yes, add this also into derived tables ) SELECT * FROM cte PIVOT (sum(NET) FOR FundType IN (' + @Columns + ')) pvt' print @Columns; -- examine the list of generated fund types PRINT @SQL; -- examine the SQL EXECUTE (@SQL);The above is from the top of my head, so may need minor adjustments.