2017-04-03 23 views
0

他のテーブルのデータから得られたかなり複雑なテーブルをそれぞれ返す2つのクエリがあります。それらを一緒に結合し、スーパー結合テーブルでグループを使用したいと思います。SQL - 2つの派生テーブルを結合する

私はこのテーブルを単一のクエリで派生させることはできません。それぞれが異なるテーブルセットにアクセスするため、ソートストーリーは、両方で必要なすべての情報を単一の行(少なくとも私は方法を考えることができません)。

最初のクエリは、次のとおりです。

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 

2番目のクエリは、次のとおりです。pr.runname、cp.firstnameにより、グループ

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に沿ってこれらに参加したい、と、 mp.name

+0

私は、結合構文を現代的なお勧めします。 – Degan

答えて

0

私はこれをあまりにも美しくしませんでしたが、これはトリックを行うべきです。 Lemmeはそれがどうなるか知っています!

select 
a.runname 
,a.firstname 
, a.name 
from 
(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)a 
inner join (
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)b 
on a.name = b.name 
group by a.runname,a.firstname, a.name 
0

あなたは(私もあなたの結合構文を変更)共通テーブル式を使用することができます。

with table_one as (
select 
    pr.runName, 
    cp.firstname, 
    mp.name 
from 
    passrun as pr 
    inner join passrunpoly as prp on pr.runid = prp.runid 
    inner join mappolygon as mp on prp.polyid = mp.polyid 
    inner join cmnemployee as ce on pr.employeeid = ce.employeeid 
    inner join cmnperson as cp on ce.personid = cp.personid 
    inner join passschedule as ps on pr.scheduleid = ps.scheduleid 
where 
    ps.ldate = 20170403 

), table_two as (

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 on pb.bookingid = pba.bookingid 
    passbookingleg as pbl on pb.bookingid = pbl.bookingid 
    mappolygon as mp on mp.polyid = pbl.addresspolygonid 
where 
    pb.ldate = 20170403 
    and pb.servicetypeid = 5 
group by mp.name, mp.abbreviation 
) 

select 
    one.runname, 
    one.name, 
    one.firstname 
from table_one one 
    inner join table_two two on one.name = two.name 
group by one.runname,one.name,one.firstname 
関連する問題