0

I have 2 queries that each return fairly complex tables derived from data in a bunch of other tables, and I would like to join them together and then use a group by on the super-joined table.

I don't think I can derive this table with a single query, because each one accesses a different set of tables, and the sort story is that it's not possible to join all of the information required by both onto a single row (at least I can't think of a way).

The first query is:

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

The second query is:

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

I want to join these along mp.name, and then group by pr.runname, cp.firstname, mp.name

user37745
  • 1
  • 1

2 Answers2

0

While I didn't make this too pretty, this should do the trick. Lemme know how it goes!

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
Eli
  • 2,538
  • 1
  • 25
  • 36
0

You can use common table expressions (I also changed your join syntax):

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
airrabian
  • 1
  • 1
  • 1