Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to filter a join
Message
 
 
To
24/04/2013 17:14:28
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
01571814
Message ID:
01571819
Views:
62
This message has been marked as the solution to the initial question of the thread.
>Hello -
>
>I am trying to write a SQL select. My goal should be obvious from my code.
>select b.RTAElement, a.tipid, rtaid, a.projectname, sponsor, developmentstatus, SUM(c.amount) as RTACommitted, SUM(d.amount) as RTASpent
>from TIP_ProjectS a 
>inner join RTA_GeneralInfo b on a.TipID = b.tipid
>left join TIP_ApprovedFunding c on a.TipID = c.tipid and c.FundType = 'RTA'
>left join TIP_Ledger d on a.TipID = d.tipid and d.FundType = 'RTA'
>group by b.RTAElement, a.tipid, rtaid, a.projectname, sponsor, developmentstatus
>order by RTAid
>
>At present, it is not correct as I want the 2 sum amounts to only include FundTypes 'RTA'. Can someone help me filter the sums but not drop any records where the sum might be null or zero?
>Thanks

Try:
select b.RTAElement, a.tipid, rtaid, a.projectname, sponsor, developmentstatus, c.RTACommitted, d.RTASpent
from TIP_ProjectS a 
inner join RTA_GeneralInfo b on a.TipID = b.tipid
left join (select TipId, SUM(amount) as RTACommited FROM TIP_ApprovedFunding WHERE FundType = 'RTA'
GROUP BY TipId) c
on a.TipID = c.tipid 
left join (SELECT TipId, SUM(Amount) AS RTASpent FROM TIP_Ledger  WHERE FundType = 'RTA'
GROUP BY TipID) d
ON d.TipID = a.TipId
order by RTAid
See this blog post
http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/aggregates-with-multiple-tables
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform