我有两个查询,每个都返回从一堆其他表中的数据派生的相当复杂的表,并且我想将它们连接在一起,然后在超级连接的表上使用一个组。SQL - 连接两个派生表
我不认为我可以用单个查询来派生此表,因为每个人访问一组不同的表,并且排序的故事是无法将所有需要的信息都加入到单个查询中行(至少我想不出一种方式)。
第一个查询是:
select
pr.runName,
cp.firstname,
mp.name
from
passrun as pr,
passrunpoly as prp,
mappolygon as mp,
cmnemployee as ce,
cmnperson as cp,
passschedule as ps
where
pr.runid = prp.runid
and prp.polyid = mp.polyid
and pr.employeeid = ce.employeeid
and ce.personid = cp.personid
and pr.scheduleid = ps.scheduleid
and ps.ldate = 20170403
第二个查询是:
select
mp.name,
count(distinct pbl.lat) as Stops,
count(case when pba.spacetype = 'S' then pb.ldate end)/2 as S,
count(case when pba.spacetype = 'WC' then pb.ldate end)/2 as WC,
count(case when pba.spacetype = 'WK' then pb.ldate end)/2 as WK
from
passbookingactivity as pba,
passbooking as pb,
passbookingleg as pbl,
mappolygon as mp
where
pb.bookingid = pba.bookingid
and pb.bookingid = pbl.bookingid
and mp.polyid = pbl.addresspolygonid
and pb.ldate = 20170403
and pb.servicetypeid = 5
group by mp.name, mp.abbreviation
我想沿着mp.name加入这些,然后按pr.runname,cp.firstname, mp.name
我推荐现代连接语法。 – Degan