我有两个查询,每个查询返回从一堆其他表中的数据派生的相当复杂的表,我想将它们连接在一起,然后在超级连接表上使用group by。
我不认为我可以使用单个查询派生此表,因为每个查询都访问一组不同的表,而排序故事是因为它无法加入所需的所有信息两者都在一排(至少我不能想到一种方式)。
第一个查询是:
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
分组答案 0 :(得分: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
答案 1 :(得分: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