Solved

Multiple aggregate functions

Posted on 2007-04-02
7
1,073 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SELECT query on two levels (detail and summary) 13 48
Help Required 2 30
insert wont work in SQL 14 19
Can > be used for a Text field 6 37
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
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.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how the fundamental information of how to create a table.

786 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