[英]How to combine those three SQL queries
How to combine this three queries without giving me the same output ? 如何在不给我相同输出的情况下合并这三个查询?
The first query is : 第一个查询是:
select
vwemployee.directorateName,
count(vwemployeeCourse.employeeId) as t1
from
vwemployee, vwemployeeCourse
where
vwemployee.directorateName = vwemployeeCourse.directorateName
group by
vwemployee.directorateName
This is the second query : 这是第二个查询:
select vwemployee.directorateName, count(vwemployee.directorateName) as t2 from vwemployee, employeeCourse where vwemployee.Id = employeeCourse.employeeId group by vwemployee.directorateName 从vwemployee,employeeCourse中选择vwemployee.directorateName,count(vwemployee.directorateName)作为t2,其中vwemployee.Id = employeeCourse.employeeId组,由vwemployee.directorateName
This is the third query : 这是第三个查询:
select
vwemployeeCourse.directorateName, sum(vwCourse.cost) as t3
from
vwemployeeCourse, vwCourse
where
vwemployeeCourse.courseId = vwCourse.Id
group by
vwemployeeCourse.directorateName
I will be using the combined query to generate a report 我将使用组合查询生成报告
the t1 column should display how many courses this specific directorate took t1栏应显示此特定首长级参加了多少门课程
the t2 column should display how many employee's under this directorate took this courses t2列应显示该局下有多少名员工参加了这些课程
the t3 column should display how much the courses cost for every directorate t3列应显示每个首长级课程的费用
So the total columns of the table of the combined query should be 4 columns 因此,组合查询表的总列数应为4列
FYI: some nice people here helped me to combine the first two queries but it was not sample at all and i didn't succeed to add the third query to them since I am a beginner so please help me with a full simple query to understand it for future references 仅供参考:这里的一些好人帮助我结合了前两个查询,但根本不是示例,由于我是初学者,所以我没有成功向他们添加第三个查询,因此请通过简单的完整查询帮助我供将来参考
I think you are looking for something like this. 我认为您正在寻找类似的东西。 We can achieve this using NESTED CTEs. 我们可以使用NESTED CTE来实现这一目标。 You can see here I have created 3 nested CTEs, and in the end I have used all the three CTEs to get your result. 您可以在这里看到我创建了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
First, you should be using explicit JOIN
syntax. 首先,您应该使用显式的JOIN
语法。 Simple rule: Never use commas in the FROM
clause. 简单规则: 请勿在FROM
子句中使用逗号。
Then, given the three queries as written, with no other information, I think I would go for full outer join
or union all
with aggregation: 然后,鉴于所写的三个查询,没有其他信息,我想我将使用聚集进行full outer join
或union 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;
Having said that, I don't think this query can actually do anything useful. 话虽如此,我认为该查询实际上无法做任何有用的事情。 Joining two tables on two different keys, and then aggregating by the same key (as for ec
and ed
) is not usually done. 通常不执行将两个表放在两个不同的键上,然后通过同一键(如ec
和ed
)进行聚合的操作。 If this doesn't produce the results you want, then ask another question, provide sample data, desired results, and a SQL Fiddle. 如果这样不能产生所需的结果,请询问另一个问题,提供示例数据,所需的结果以及SQL Fiddle。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.