我有兩個查詢,每個都返回從一堆其他表中的數據派生的相當複雜的表,並且我想將它們連接在一起,然後在超級連接的表上使用一個組。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