Solved

Multiple aggregate functions

Posted on 2007-04-02
7
1,075 Views
Last Modified: 2007-12-19
I am trying to write a clean, simple query to get some aggregate data.  For each person, I want to know the sum of their allocated hours and the sum of their logged hours.  They can be assigned multiple tasks and they can have multiple hours entries for each task.

The tables are much more complex than this, but I think this is enough to ask my question:

Person
----------
Person_pri
Name

Task
------
Task_pri
AllocatedHours

TaskAssigned
--------
Task_pri
Person_pri

Hours
---------
Task_pri
Person_pri
Hours

Right now, I have this
SELECT Person.name, sum(Task.AllocatedHours), sum(Hours.Hours)
FROM Person
INNER JOIN TaskAssigned ON Person.Person_pri = TaskAssigned.Person_pri
INNER JOIN Task ON TaskAssigned.Task_pri = Task.Task_pri
INNER JOIN Hours ON Task.Task_pri = Hours.Task_pri AND Person.Person_pri = Hours.Person_pri
GROUP BY Person.name


Now, the obvious problem here is that if there are multiple hours records per task, the AllocatedHours for each task will be added multiple times.  So in this case, the sum(Hours.Hours) comes out right, but the sum(Task.AllocatedHours) is huge.  If I remove the join on Hours, that number is right, but of course I lose the sum on the Hours.Hours.  

Is there any way to get all this into one query?

Thanks,
Christie
0
Comment
Question by:muellerfan
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 400 total points
ID: 18841682
use  outer joins and sub queries

more like this...


SELECT P.Person.name, sum(T.AllocatedHours), sum(H.Hours)
FROM Person as P
Left Outer Join (select person_pri,sum(allocatedhours) as Hours
                            from TaskAssigned as ta
                          Inner Join task as t
                              on ta.task_pri=t.task_pri
                          group by person_pri) as T
 ON P.Person_pri = T.Person_pri
Left Outer Join (select person_pri,sum(hours) as Hours
                       from Hours
                    group by person_pri) as H
 ON P.Person_pri = H.Person_pri
GROUP BY Person.name



0
 
LVL 14

Expert Comment

by:mherchl
ID: 18841709
Try this:

SELECT a.name, b.sum_alloc, isnull(c.sum_hours, 0) as sum_hours
  FROM Person a
  JOIN (SELECT ax.Person_pri, SUM(bx.AllocatedHours) as sum_alloc FROM TaskAssigned ax JOIN Task bx on ax.Task_pri = bx.Task_pri GROUP BY ax.Person_pri) b on a.Person_pri = b.Person_pri
LEFT OUTER JOIN (SELECT ax.Person_pri, SUM(ax.Hours) as sum_hours FROM Hours) c on a.Person_pri = c.Person_pri
0
 
LVL 2

Expert Comment

by:jhshen
ID: 18841711
Just some syntax correction for Lowfatspread's query to make it work properly. :)

SELECT P.name, sum(T.AllocatedHours), sum(H.Hours)
FROM Person as P
Left Outer Join (select person_pri,sum(allocatedhours) as AllocatedHours
                            from TaskAssigned as ta
                          Inner Join task as t
                              on ta.task_pri=t.task_pri
                          group by person_pri) as T
 ON P.Person_pri = T.Person_pri
Left Outer Join (select person_pri,sum(hours) as Hours
                       from Hours
                    group by person_pri) as H
 ON P.Person_pri = H.Person_pri
GROUP BY P.name
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 2

Assisted Solution

by:jhshen
jhshen earned 100 total points
ID: 18841720
This will be a more completed query.

SELECT P.name, isnull(sum(T.AllocatedHours), 0) as sum_AllocatedHours, isnull(sum(H.Hours), 0) as sum_Hours
FROM Person as P
Left Outer Join (select person_pri,sum(allocatedhours) as AllocatedHours
                            from TaskAssigned as ta
                          Inner Join task as t
                              on ta.task_pri=t.task_pri
                          group by person_pri) as T
 ON P.Person_pri = T.Person_pri
Left Outer Join (select person_pri,sum(hours) as Hours
                       from Hours
                    group by person_pri) as H
 ON P.Person_pri = H.Person_pri
GROUP BY P.name
0
 
LVL 1

Author Comment

by:muellerfan
ID: 18846184
Thanks for the suggestions.  I will follow this concept.  The only thing is that I need to join the Hours table with the Task table.  I don't want all hours for that person, but hours that are associated with a Task.

I will play around with it on my own, but if you have suggestions, I would appreciate it.

Thanks again.
0
 
LVL 1

Author Comment

by:muellerfan
ID: 18846836
I ended up just making larger subqueries.  Thanks again for your help!
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 18847542
then proably

from person as p
inner join task_assigned as ta
 on p.person_pri=ta.person_pri
 Left outer join ( select person_pri,task_pri,sum(hours) ...
         ) as h
 on p.person_pri=h.person_pri
and ta.task_pri=h.task_pri
left outer join (select ...)

.
0

Featured Post

Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how the fundamental information of how to create a table.

726 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