Solved

MS Access Sum Query- need a 0 return

Posted on 2008-10-29
13
175 Views
Last Modified: 2012-06-21
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-Hold" 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
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"));

Open in new window

0
Comment
Question by:RalphyC
  • 5
  • 4
  • 4
13 Comments
 
LVL 35

Expert Comment

by:Terry Woods
ID: 22835964
Might not be pretty, but this should work:
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

GROUP BY tblProjects.ProjectDisposition

HAVING (tblProjects.ProjectDisposition)="Pending ITSC approval/prioritization"

    OR (tblProjects.ProjectDisposition)="Completed"

    OR (tblProjects.ProjectDisposition)="Cancelled"

    OR (tblProjects.ProjectDisposition)="On-Hold"

)

Open in new window

0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 22835970
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

)

Open in new window

0
 
LVL 5

Expert Comment

by:jfmador
ID: 22835995
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_CostEstimate,0)) then if you get null for the tblSizing.CC_CostEstimate it will be replace by 0 then the sum will give you 0
0
 
LVL 5

Expert Comment

by:jfmador
ID: 22836019
Forget about my coalesce it didn't exists under access, terry is right you should use IIF and ISNULL

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"

Open in new window

0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 22836026
Was just about to suggest that myself, thanks to your first suggestion! :-)
0
 
LVL 5

Expert Comment

by:jfmador
ID: 22836144
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 :)
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:RalphyC
ID: 22836155
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-Hold" 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
0
 
LVL 5

Expert Comment

by:jfmador
ID: 22836201
ok then you need a table with a row containing ProjectDisposition = "On-Hold"

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" 

Open in new window

0
 

Author Comment

by:RalphyC
ID: 22836240
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.
0
 

Author Comment

by:RalphyC
ID: 22836278
Actually all now I need to so is display the distinct records, since they are repeating well over 100 times.
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
ID: 22836293
I think jfmador's initial suggestion for the query is ok, as long as the filters in the "having" section are shifted to the "where" section
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

  and ( (tblProjects.ProjectDisposition)="Pending ITSC approval/prioritization"

        OR (tblProjects.ProjectDisposition)="Completed"

        OR (tblProjects.ProjectDisposition)="Cancelled"

        OR (tblProjects.ProjectDisposition)="On-Hold"

      )

GROUP BY tblProjects.ProjectDisposition

Open in new window

0
 

Author Comment

by:RalphyC
ID: 22836296
Got it, just add DISTINCT and it will do the trick.
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 22836342
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.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to do this MS SQL script? 11 56
sql query help 4 45
SQL join help to a thrid table 51 76
Oracle Pivot Question 8 45
Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now