如何组合这三个SQL查询

时间:2016-05-15 11:53:10

标签: sql sql-server-2008

如何在不给我相同输出的情况下组合这三个查询?

第一个查询是:

select   
    vwemployee.directorateName,
    count(vwemployeeCourse.employeeId) as t1 
from 
    vwemployee, vwemployeeCourse 
where  
    vwemployee.directorateName = vwemployeeCourse.directorateName 
group by 
    vwemployee.directorateName

这是第二个查询:

选择        vwemployee.directorateName,        count(vwemployee.directorateName)为t2     从        vwemployee,employeeCourse     哪里        vwemployee.Id = employeeCourse.employeeId     通过...分组        vwemployee.directorateName

这是第三个查询:

select 
    vwemployeeCourse.directorateName, sum(vwCourse.cost) as t3
from 
    vwemployeeCourse, vwCourse
where 
    vwemployeeCourse.courseId = vwCourse.Id
group by 
    vwemployeeCourse.directorateName 

我将使用合并查询生成报告

  • t1列应显示此特定董事会的课程数量

  • t2栏应显示该局下属的员工参加此课程的次数

  • t3栏应显示每个董事会的课程费用

因此,组合查询的表的总列数应为4列

FYI:这里的一些好人帮我合并了前两个查询,但它根本没有样本,我没有成功将第三个查询添加到他们,因为我是初学者所以请帮我完整的简单查询,以便将来了解它

2 个答案:

答案 0 :(得分:2)

我认为你正在寻找这样的东西。我们可以使用NESTED CTE实现这一目标。你可以在这里看到我创建了3个嵌套的CTE,最后我使用了所有三个CTE来获得你的结果。

with cte1 as
(
select   vwemployee.directorateName   , count(vwemployeeCourse.employeeId) as t1 

from vwemployee , vwemployeeCourse 

where  vwemployee.directorateName = vwemployeeCourse.directorateName 

GROUP BY vwemployee.directorateName
)
,cte2 as
(
select vwemployee.directorateName , count(vwemployee.directorateName) as t2 

from vwemployee , employeeCourse

where vwemployee.Id = employeeCourse.employeeId 

GROUP BY  vwemployee.directorateName
)
,cte3 as
(
 select vwemployeeCourse.directorateName , sum(vwCourse.cost) as t3

 from vwemployeeCourse , vwCourse

 where vwemployeeCourse.courseId = vwCourse.Id

 group by vwemployeeCourse.directorateName 
)
select cte1.directorateName, cte1.t1, cte2.t2, cte3.t3
from
cte1 inner join cte2 
on cte1.directorateName = cte2.directorateName
inner join cte3 on
cte2.directorateName = cte3.directorateName

答案 1 :(得分:0)

首先,您应该使用显式JOIN语法。简单的规则:从不FROM子句中使用逗号。

然后,如果写了三个查询,没有其他信息,我想我会选择汇总full outer joinunion all

with ec as (
      select e.directorateName, count(ec.employeeId) as t1
      from vwemployee e join
           vwemployeeCourse ec
           on e.directorateName = ec.directorateName 
      group by e.directorateName
    ),
    ed as (
      select e.directorateName, count(ec.directorateName) as t2
      from vwemployee e join
           vwemployeeCourse ec
           on e.id = ec. employeeId 
      group by e.directorateName
     ),
     cc as (
      select ec.directorateName, sum(c.cost) as t3
      from vwemployeeCourse ec join
           vwCourse c
           ec.courseId = c.Id
      group by ec.directorateName 
     )
select directoratename,
       coalesce(t1, 0) as t1,
       coalesce(t2, 0) as t2,
       coalesce(t3, 0) as t3
from ((select directoratename, t1, null as t2, null as t3 from ec)
      union all
      (select directoratename, null as t1, t2, null as t3 from ed)
      union all
      (select directoratename, null as t1, null as t2, t3 from cc)
     ) t;

话虽如此,我不认为这个查询实际上可以做任何有用的事情。在两个不同的密钥上连接两个表,然后通过相同的密钥(对于eced进行聚合)通常不会。如果这不能产生您想要的结果,那么请提出另一个问题,提供样本数据,所需结果和SQL小提琴。