在一个查询中计算两个不同表中的两个不同字段

时间:2010-02-11 22:11:53

标签: sql

我需要以某种方式将这两个查询放入一个查询中:

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)  

除了来自计算不同字段的不同表的计数之外,一切都基本相同。我需要将结果显示为一个显示容量的表和#filled

3 个答案:

答案 0 :(得分:1)

您可以使用Union语句,只为每个语句添加一个空列。像这样:

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) 

如果由于您无法执行组操作而无法正常工作,请尝试将行插入包含capacity和#filled列的临时表中。你只需要为每个插入留一个空。如果你正在寻找的是两行结果,那么我会做同样的事情,除了将结果插入2个独立的临时表,并在他们独特的列上进行OUTER JOIN,我认为这将是fac_name,month和year。评论我的帖子,如果这不起作用或不是你想要做的。

答案 1 :(得分:1)

左边连接会更容易。

就是这样,会发生这样的事情:

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]

答案 2 :(得分:0)

你不能只使用SQL Union运算符吗? SQL Union Operator