Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Query on table with one to many relationship

Author  Topic 

PolarBear
Starting Member

5 Posts

Posted - 2009-02-17 : 13:06:10
I need to run a query/script based on data similar to below:

Hours_ID Memb_ID Work_Hrs Home_Hrs
456 1234 40 15
786 1234 20
621 1234 10
452 652 10 60
442 652 15 30

I need to total all hours as follows and include a Total_Hours column as follows:

Memb_ID Work_Hrs Home_Hrs Total_Hhours
1234 60 25 105
652 25 90 115


Any assistance would be appreciated

mfemenel
Professor Frink

1421 Posts

Posted - 2009-02-17 : 13:14:23
select memb_id,sum(work_hours) WorkHours,sum(home_hours)HomeHours,sum(work_hours + home_hours)TotalHours
from #temp
group by memb_id

Mike
"oh, that monkey is going to pay"
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-02-17 : 13:43:36
Your Expected output should be:
Memb_ID     Work_Hrs    Home_Hrs  Total_Hhours
1234 60 25 85
652 25 90 115
Go to Top of Page

PolarBear
Starting Member

5 Posts

Posted - 2009-02-17 : 16:23:53
Thanks for the quick reply. That was a great help!

PolarBear
Go to Top of Page
   

- Advertisement -