Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
bogey
Posting Yak Master
166 Posts |
Posted - 2004-07-22 : 11:45:45
|
| Situation is I'm summing a bunch of columns from different tables and grouping everything together on a ssn. Using a left join I'm getting null in cols that do not have a coresponding link. I then use the case stmt to check for a null and if so replace with a zero. Now I'd like to add all the columns together but I'm getting a null value if any of the columns had a null but where changed to zero. Attached is the code.SELECT dbo.tblFacInfo.[Last Name], dbo.tblFacInfo.[First Name], dbo.tblFacInfo.SSN,case when a1.IncentivePaymentSum is Null then 0 else a1.IncentivePayMentSumend as IncentivePaymentSum, case when a2.AcademicPaymentSum is Null then 0 else a2.AcademicPaymentSumend as AcademicPaymentSum,case when a3.ResearchPaymentSum is Null then 0 else a3.ResearchPaymentSumend as ResearchPaymentSum,case when a4.SuppPaymentSum is Null then 0 else a4.SuppPaymentSumend as SuppPaymentSum,--Add all Values together but get error if any col has a nulla1.IncentivePaymentSum + a2.AcademicPaymentSum + a3.ResearchPaymentSum +a4.SuppPaymentSum as TotalFROM dbo.tblFacInfo LEFT OUTER JOIN (SELECT SSN, SUM(AmountPd) AS AcademicPaymentSum FROM dbo.tblAcademicPmts GROUP BY SSN) a2 ON dbo.tblFacInfo.SSN = a2.SSN LEFT OUTER JOIN (SELECT SSN, SUM(AmountPd) AS IncentivePaymentSum FROM dbo.tblIncentivePmts GROUP BY SSN) a1 ON dbo.tblFacInfo.SSN = a1.SSN LEFT OUTER JOIN (SELECT SSN, SUM(AmountPd) AS ResearchPaymentSum FROM dbo.tblResearchPmts GROUP BY SSN) a3 ON dbo.tblFacInfo.SSN = a3.SSN LEFT OUTER JOIN (SELECT SSN, SUM(AmountPd) AS SuppPaymentSum FROM dbo.tblSuppPmts GROUP BY SSN) a4 ON dbo.tblFacInfo.SSN = a4.SSN order by dbo.tblFacInfo.SSN |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2004-07-22 : 11:53:37
|
| all formulas reference the original columns, not the new alias's or formulas you have calculated. so, you need to do the calculations more twice if you'd like to reference it twice.A handy trick for you is to use the ISNULL() function like so:SELECT dbo.tblFacInfo.[Last Name], dbo.tblFacInfo.[First Name], dbo.tblFacInfo.SSN,ISNULL(a1.IncentivePaymentSum,0) as IncentivePaymentSum, ISNULL(a2.AcademicPaymentSum,0) as AcademicPaymentSum,ISNULL(a3.ResearchPaymentSum,0) as ResearchPaymentSum,ISNULL(a4.SuppPaymentSum,0) as SuppPaymentSum,--Add all Values together but get error if any col has a nullISNULL(a1.IncentivePaymentSum,0) + ISNULL(a2.AcademicPaymentSum,0) +ISNULL(a3.ResearchPaymentSum,0) + ISNULL(a4.SuppPaymentSum,0) as Totalanother option, by the way is to use a subquery:select a.*, IncentivePaymentSum+AcademicPaymentSum+ResearchPaymentSum+ResearchPaymentSum as Totalfrom (-- your query here, w/o the total calculated --) aThen, in this case, you can reference the new alias's since they live in the subquery. I hope this makes sense.- Jeff |
 |
|
|
bogey
Posting Yak Master
166 Posts |
Posted - 2004-07-22 : 13:39:11
|
| Jeff, Worked like a charm :)thanksDan |
 |
|
|
|
|
|
|
|