We help IT Professionals succeed at work.

SQL code is not summarizing totals by group

278 Views
Last Modified: 2014-05-21
I know I'm missing something simple, but am not seeing it.  I'm starting with a table (called view_project_with_attributes) and it has one record per fund - a total of about 4100 funds.  I'm trying to bring in the balances for a couple of account codes so have done a sub query where I am summing the dollars and grouping by the projectdimid (which is the fund).  Instead of getting 4100 records, I'm getting over a million.  I've tried doing this code multiple ways and it just isn't working.  What am I missing?

SELECT        View_Project_with_Attributes.ProjectDimID, View_Project_with_Attributes.ProjectID, View_Project_with_Attributes.ProjectDescription,
                         View_Project_with_Attributes.ProjClass, View_Project_with_Attributes.ProjCollege, View_Project_with_Attributes.ProjDept, Q1400.Balance1400,
                         Q1405.Balance1405

FROM            View_Project_with_Attributes
LEFT OUTER JOIN
                             (SELECT        ProjectDimID, SUM(NaturalAmount) AS Balance1400
                               FROM            FACT_GLTransactionDistribution
                               WHERE        (AccountCode = '1400') AND (PostDate <= '3/31/14') AND (AccountCategorySystemID IN ('1'))
                               GROUP BY ProjectDimID) AS Q1400 ON View_Project_with_Attributes.ProjectDimID = Q1400.ProjectDimID
LEFT OUTER JOIN
                             (SELECT        ProjectDimID, SUM(NaturalAmount) AS Balance1405
                               FROM            FACT_GLTransactionDistribution AS FACT_GLTransactionDistribution_1
                               WHERE        (AccountCode = '1405') AND (PostDate >= '7/1/01') AND (PostDate <= '3/31/14') AND (AccountCategorySystemID IN ('1'))
                               GROUP BY ProjectDimID) AS Q1405 ON View_Project_with_Attributes.ProjectDimID = Q1400.ProjectDimID

HAVING        (View_Project_with_Attributes.ProjClass IN ('Endowed', 'Quasi Endowed', 'Endowed - Non Invested'))
Comment
Watch Question

CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
cindyfillerDirector of IT

Author

Commented:
You are right.  I know better than that - too many things going on.  Thanks for the catch on this.
cindyfillerDirector of IT

Author

Commented:
Very quick reply and perfect answer.
PortletPaulEE Topic Advisor
CERTIFIED EXPERT
Most Valuable Expert 2014
Awarded 2013

Commented:
in support only:

a "having clause" is for conditions that can only exist after the group by is applied
e.g. having sum(value) > 100
that sum(...) can only be evaluated after grouping the data
CERTIFIED EXPERT

Commented:
That was quick, on my screen the date/times look like I answered before you asked the question?
Asked on	2014-05-21 at 16:17:14 ID: 28439127
Answered	2014-05-21 at 14:55:48 ID: 40082022

Open in new window

PortletPaulEE Topic Advisor
CERTIFIED EXPERT
Most Valuable Expert 2014
Awarded 2013

Commented:
you are a qualified mind reader? very useful
cindyfillerDirector of IT

Author

Commented:
Hey - I needed an answer fast and you gave it super fast!  Just didn't realize how fast.  Thanks again for the assist.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.