tags:

views:

91

answers:

3

I need to somehow put these two queries into one query:

1

select fac_name, datename(month,b.startdt) as 'month', year(b.startdt) as 'year', count(a.empfk) as '#filled'  
from tbl_tmx_attempt a  
left outer join tbl_tmx_activity b on a.activityfk = b.activity_pk  
left outer join tbl_tmx_actloc c on b.activity_pk = c.activityfk  
left outer join loc d on c.locfk = d.loc_pk  
left outer join fac e on d.loc_facfk = e.fac_pk  
where b.startdt > '12/31/08'    
group by fac_name, year(b.startdt), month(b.startdt), datename(month,b.startdt) 

2

select fac_name, datename(month,b.startdt) as 'month', year(b.startdt) as 'year', sum(b.maxcapacity) as 'capacity'  
from tbl_tmx_activity b   
left outer join tbl_tmx_actloc c on b.activity_pk = c.activityfk  
left outer join loc d on c.locfk = d.loc_pk  
left outer join fac e on d.loc_facfk = e.fac_pk  
where b.startdt > '12/31/08'  
group by fac_name, year(b.startdt), month(b.startdt), datename(month,b.startdt)  

Everything is basically the same except the counts are from different tables counting a different field. I need the results to be one table showing the capacity and #filled

A: 

Can't you just use a SQL Union operator? SQL Union Operator

Kevin Gale
i tried actually and it only gives me one of the count fields that i need. . .unless i'm doing it wrong . . but i have used union before. maybe since i need it to return two different fields from two different tables?
charly
That's why I was hesitant with the answer it seemed too obvious. :-) I think I see the problem the count fields are have different names. You would really need to use the same column name.
Kevin Gale
Or you could return both columns in both queries. Just return a zero in the query where it isn't relevant.
Kevin Gale
Union will give you two rows, not two columns.
Eric J.
Of course, but the problem is the queries are the same except the name of one column. If you add both colums to both queries such as the answer by Randdom Ben or Peter does then you get back both columns in both queries so a union then works.
Kevin Gale
+1  A: 

You could use a Union statement and just add a null column in for each. Like this:

select fac_name, datename(month,b.startdt) as 'month', year(b.startdt) as 'year', count(a.empfk) as '#filled' , '' as 'capacity' 
from tbl_tmx_attempt a  
left outer join tbl_tmx_activity b on a.activityfk = b.activity_pk  
left outer join tbl_tmx_actloc c on b.activity_pk = c.activityfk  
left outer join loc d on c.locfk = d.loc_pk  
left outer join fac e on d.loc_facfk = e.fac_pk  
where b.startdt > '12/31/08'    
group by fac_name, year(b.startdt), month(b.startdt), datename(month,b.startdt)

Union All

select fac_name, datename(month,b.startdt) as 'month', year(b.startdt) as 'year', '' as '#filled', sum(b.maxcapacity) as 'capacity'  
from tbl_tmx_activity b   
left outer join tbl_tmx_actloc c on b.activity_pk = c.activityfk  
left outer join loc d on c.locfk = d.loc_pk  
left outer join fac e on d.loc_facfk = e.fac_pk  
where b.startdt > '12/31/08'  
group by fac_name, year(b.startdt), month(b.startdt), datename(month,b.startdt) 

If this fails to work because you can't do a group by then try inserting the rows into a temp table with both capacity and #filled columns. You would just leave one empty for each insert. If what you are looking for is 1 row with both results then I would do the same as above except insert the results into 2 separate temp tables and do an OUTER JOIN on their unique Columns which I think would be fac_name, month, and year. Comment on my post if that doesn't work or isn't what you are trying to do.

RandomBen
Thanks so much. . . the second suggestion worked.
charly
+1  A: 

it would be easier w/ out the left joins.

as is, something like this would do:

select fac_name, [month], [year], max([#filled]) as [#filled], max(capacity) as [capacity]

from (

  select fac_name, datename(month,b.startdt) as 'month', year(b.startdt) as 'year', count(a.empfk) as '#filled', null as 'capacity'
  from tbl_tmx_attempt a  
  left outer join tbl_tmx_activity b on a.activityfk = b.activity_pk  
  left outer join tbl_tmx_actloc c on b.activity_pk = c.activityfk  
  left outer join loc d on c.locfk = d.loc_pk  
  left outer join fac e on d.loc_facfk = e.fac_pk  
  where b.startdt > '12/31/08'    
  group by fac_name, year(b.startdt), month(b.startdt), datename(month,b.startdt) 

  union all

  select fac_name, datename(month,b.startdt) as 'month', year(b.startdt) as 'year', null as '#filled', sum(b.maxcapacity) as 'capacity'  
  from tbl_tmx_activity b   
  left outer join tbl_tmx_actloc c on b.activity_pk = c.activityfk  
  left outer join loc d on c.locfk = d.loc_pk  
  left outer join fac e on d.loc_facfk = e.fac_pk  
  where b.startdt > '12/31/08'  
  group by fac_name, year(b.startdt), month(b.startdt), datename(month,b.startdt)  

  ) a

group by fac_name, [month], [year]
Peter
Good point on the Union All. It is much faster than Union. Use that.
RandomBen
Thank you Peter!!!!!!! This works perfectly!!!!
charly