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