Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Multiple aggregate functions

Posted on 2007-04-02
7
1,074 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

792 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