Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Some fancy SQL for TIP Amendment
Message
De
27/08/2013 19:39:53
 
 
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Versions des environnements
SQL Server:
SQL Server 6.5 and older
Application:
Web
Divers
Thread ID:
01581564
Message ID:
01581581
Vues:
36
Thanks Naomi, I figured if it could be done I would get the help here. There is one additional quirk that I failed to mention. I need to select a specific AmendmentNumber from both tables. So in my real world case I ended up with this:
;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.

APPROVED
Keyid AmendmentNumber TipID AMOUNT FundType YEAR PHASE
296 2014.01 5.12 8000 NH 2013 DR
297 2014.01 5.12 16500 RTA 2017 D
298 2014.01 5.12 6500 ASTP 2017 D

PROPOSED
Keyid AmendmentNumber TipID AMOUNT FundType YEAR PHASE
332 2014.01 5.12 8000 NH 2013 DR
333 2014.01 5.12 16500 RTA 2017 D
334 2014.01 5.12 6500 ASTP 2017 D
336 2014.01 5.12 42900 RTA 2018 C
337 2014.01 5.12 43583 ASTP 2018 C

For this project, the resultant difference for ASTP is 37083, whereas it should be 43583 because the two 6500 values cancel each other out. So maybe there needs to be a couple of sums and group by in the first part of the select? It is still fairly greek to me.

Its interesting that the keyword PIVOT does not appear in my 800 page SQL handbook. Neither does pvt which I assume is just an alias assigned to the result. Correct?
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform