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
 General SQL Server Forums
 New to SQL Server Programming
 SQL JOIN

Author  Topic 

david_mac
Starting Member

2 Posts

Posted - 2009-09-15 : 18:40:27
Hi,

I would appreciate any help I can get with this, I'm sure it's probably quite easy for someone out there.

I have 5 tables, I am wanting to write 1 query that would do a total of one column from each table then a 'full' total of each of those subtotals. I have done this previously when all data was in one table, but since breaking the tables up i've had considerable problems.

I've tried the below UNION, however I only get the subtotal of one column 'Admin_staff_total - which displays a full total of each of the fields.

"select university, hospital,
sum(a.number_of_staff) as Admin_staff_total from Admin_staff_at_Hospital a WHERE hospital = '$hospital' GROUP BY university
UNION
select university, hospital, sum(c.number_of_staff) as Clinical_staff_total from Clinical_staff_at_Hospital c WHERE hospital = '$hospital' GROUP BY university
UNION
select university, hospital, sum(r.number_of_staff) as Research_staff_total from Research_staff_at_Hospital r WHERE hospital = '$hospital' GROUP BY university
UNION
select university, hospital, sum(cs.number_of_students) as Coursework_stud_total from Coursework_students_at_Hospital cs WHERE hospital = '$hospital' GROUP BY university
UNION
select university, hospital, sum(rs.number_of_students) as Res_stud_total from Research_students_at_Hospital rs WHERE hospital = '$hospital' GROUP BY university";

Any help or ideas would be awesome, I've tried JOINS also but no prevail - but that could be because of my incorrectness.

Thanks
David

winterh
Posting Yak Master

127 Posts

Posted - 2009-09-16 : 06:07:24
UNION wont work here.

You could create a derived table or a temp table to put all the data you want into before you count it, but this would slow the report down.

If you want to know how I would do it let me know.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-09-16 : 12:32:09
slight tweak:-

select university, hospital,
sum(case when category='Admin' then memcount else 0 end) as Admin_staff_total,
sum(case when category='Clinical' then memcount else 0 end) as Clinical_staff_total,
sum(case when category='ResearchStaff' then memcount else 0 end) as Research_staff_total,
sum(case when category='Coursework' then memcount else 0 end) as Coursework_stud_total,
sum(case when category='ResearchStudent' then memcount else 0 end) as Res_stud_total
from
(
select university, hospital,
a.number_of_staff as memcount,'Admin' as Category
from Admin_staff_at_Hospital a
WHERE hospital = '$hospital'

UNION ALL

select university, hospital, c.number_of_staff,'Clinical'
from Clinical_staff_at_Hospital c
WHERE hospital = '$hospital'

UNION ALL

select university, hospital, r.number_of_staff,'ResearchStaff'
from Research_staff_at_Hospital r
WHERE hospital = '$hospital'

UNION ALL

select university, hospital, cs.number_of_students,'Coursework'
from Coursework_students_at_Hospital cs
WHERE hospital = '$hospital'

UNION ALL

select university, hospital, rs.number_of_students,'ResearchStudent'
from Research_students_at_Hospital rs
WHERE hospital = '$hospital'
)t
group by university, hospital
Go to Top of Page

david_mac
Starting Member

2 Posts

Posted - 2009-09-17 : 23:47:55
Thank you visakh16 for your 'slight tweak' - this worked beautifully!!!

Go to Top of Page
   

- Advertisement -