RalphyC
asked on
MS Access Sum Query- need a 0 return
I have the following query below. The items are listed by Grouping, there is 4 possible groupings. i am attempted to sum up SumOfCC_CostEstimate by Grouping. The count query works fine for each item that has least onc record, but ignores records where there is not matches.
I want to dislay all the possible groups, and if there is no records I want to show 0 in the SumOfCC_CostEstimate, in my example thewre is no records matching ProjectDisposition="On-Hol d" and tblProjects.NLG)=Yes. I want to disaply
ProjectDisposition SumOfCC_CostEstimate
Cancelled 128000
Completed 30000
Pending ITSC approval/prioritization 130000
On-Hold 0 ' This is missing
I want to dislay all the possible groups, and if there is no records I want to show 0 in the SumOfCC_CostEstimate, in my example thewre is no records matching ProjectDisposition="On-Hol
ProjectDisposition SumOfCC_CostEstimate
Cancelled 128000
Completed 30000
Pending ITSC approval/prioritization 130000
On-Hold 0 ' This is missing
SELECT tblProjects.ProjectDisposition, Sum(tblSizing.CC_CostEstimate) AS SumOfCC_CostEstimate
FROM (tblProjects LEFT JOIN tLUProjectStatus ON tblProjects.ProjectDisposition = tLUProjectStatus.DispositionDescription) LEFT JOIN tblSizing ON tblProjects.PID = tblSizing.PID
WHERE (((tblProjects.NLG)=Yes)) OR (((tblProjects.NLG)=Yes)) OR (((tblProjects.NLG)=Yes)) OR (((tblProjects.NLG)=Yes))
GROUP BY tblProjects.ProjectDisposition
HAVING (((tblProjects.ProjectDisposition)="Pending ITSC approval/prioritization")) OR (((tblProjects.ProjectDisposition)="Completed")) OR (((tblProjects.ProjectDisposition)="Cancelled")) OR (((tblProjects.ProjectDisposition)="On-Hold"));
Or slightly better:
select ProjectDisposition, iif(isnull(SumOfCC_CostEstimate),0,SumOfCC_CostEstimate)
from (
SELECT tblProjects.ProjectDisposition,
Sum(tblSizing.CC_CostEstimate) AS SumOfCC_CostEstimate
FROM tblProjects
LEFT JOIN tLUProjectStatus ON tblProjects.ProjectDisposition = tLUProjectStatus.DispositionDescription
LEFT JOIN tblSizing ON tblProjects.PID = tblSizing.PID
WHERE tblProjects.NLG=Yes
and ( (tblProjects.ProjectDisposition)="Pending ITSC approval/prioritization"
OR (tblProjects.ProjectDisposition)="Completed"
OR (tblProjects.ProjectDisposition)="Cancelled"
OR (tblProjects.ProjectDisposition)="On-Hold"
)
GROUP BY tblProjects.ProjectDisposition
)
If I understand your problem you do have a row in your tblProjects for the On-Hold, but you don't have any data in tblSizing, so the sum return you null?
If you get a null value, simply add a coalesce in your sum : Sum(COALESCE(tblSizing.CC_ CostEstima te,0)) then if you get null for the tblSizing.CC_CostEstimate it will be replace by 0 then the sum will give you 0
If you get a null value, simply add a coalesce in your sum : Sum(COALESCE(tblSizing.CC_
Forget about my coalesce it didn't exists under access, terry is right you should use IIF and ISNULL
but I would suggest
but I would suggest
SELECT tblProjects.ProjectDisposition,
Sum(IIF(ISNULL(tblSizing.CC_CostEstimate),0,tblSizing.CC_CostEstimate)) AS SumOfCC_CostEstimate
FROM (tblProjects
LEFT JOIN tLUProjectStatus ON tblProjects.ProjectDisposition = tLUProjectStatus.DispositionDescription)
LEFT JOIN tblSizing ON tblProjects.PID = tblSizing.PID
WHERE tblProjects.NLG=Yes
GROUP BY tblProjects.ProjectDisposition
HAVING (tblProjects.ProjectDisposition)="Pending ITSC approval/prioritization"
OR (tblProjects.ProjectDisposition)="Completed"
OR (tblProjects.ProjectDisposition)="Cancelled"
OR (tblProjects.ProjectDisposition)="On-Hold"
Was just about to suggest that myself, thanks to your first suggestion! :-)
Yeah I really like COALESCE, I never know how to prononce it and what it really means, Is it a real word? enyway it is a neat function.
sorry i'm french :)
sorry i'm french :)
ASKER
jfmador
Using your last example, I am still getting only 3 rows returned. To make this clear, tblprojects does not have any records that match (ProjectDisposition="On-Ho ld" and tblProjects.NLG=Yes). Table tblSizing is only used for purpose of extracting the CC_CostEstimate if it has matching PID from table tblProjects, and it is a 1 to 1 relationship with tblProjects. Please advise.
ProjectDisposition SumOfCC_CostEstimate
Cancelled 128000
Completed 30000
Pending ITSC approval/prioritization 130000
On-Hold 0 ' This is still missing
Using your last example, I am still getting only 3 rows returned. To make this clear, tblprojects does not have any records that match (ProjectDisposition="On-Ho
ProjectDisposition SumOfCC_CostEstimate
Cancelled 128000
Completed 30000
Pending ITSC approval/prioritization 130000
On-Hold 0 ' This is still missing
ok then you need a table with a row containing ProjectDisposition = "On-Hold"
you can use the same table twice to do it
you can use the same table twice to do it
SELECT tblProjects.ProjectDisposition, IIF(ISNULL(SumOfCC_CostEstimate),0,SumOfCC_CostEstimate)
FROM tblProjects LEFT JOIN (
SELECT tblProjects.ProjectDisposition,
Sum(tblSizing.CC_CostEstimate) AS SumOfCC_CostEstimate
FROM (tblProjects
LEFT JOIN tLUProjectStatus ON tblProjects.ProjectDisposition = tLUProjectStatus.DispositionDescription)
LEFT JOIN tblSizing ON tblProjects.PID = tblSizing.PID
WHERE tblProjects.NLG=Yes
GROUP BY tblProjects.ProjectDisposition
) t on tblProjects.ProjectDisposition = t.ProjectDisposition
WHERE (tblProjects.ProjectDisposition) = "Pending ITSC approval/prioritization"
OR (tblProjects.ProjectDisposition)="Completed"
OR (tblProjects.ProjectDisposition)="Cancelled"
OR (tblProjects.ProjectDisposition)="On-Hold"
ASKER
This works, but it totals all records from table projects, regardelss of the tblProjects.NLG=Yes. If I add this to the criteria tblProjects.NLG=Yes, the On-Hold ), no longer appears.
ASKER
Actually all now I need to so is display the distinct records, since they are repeating well over 100 times.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Got it, just add DISTINCT and it will do the trick.
I think jfmador deserves some points - if you post a request to reopen the question in the community support zone with the url of this question, they will reopen it so you can split the points.
Open in new window