Solved

Multiple aggregate functions

Posted on 2007-04-02
7
1,070 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

757 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

19 Experts available now in Live!

Get 1:1 Help Now