【问题标题】:SQL - Joining 2 derived tablesSQL - 连接 2 个派生表
【发布时间】:2017-04-03 21:34:27
【问题描述】:

我有 2 个查询,每个查询都返回从一堆其他表中的数据派生的相当复杂的表,我想将它们连接在一起,然后在超级连接的表上使用 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 分组

【问题讨论】:

  • 我推荐现代连接语法。

标签: sql join derived-table


【解决方案1】:

虽然我没有把它做得太漂亮,但这应该可以解决问题。 让我知道它是怎么回事!

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

【讨论】:

    【解决方案2】:

    您可以使用公用表表达式(我还更改了您的连接语法):

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-24
      • 2016-09-18
      • 2018-03-12
      • 1970-01-01
      • 1970-01-01
      • 2014-12-28
      • 2023-03-05
      • 1970-01-01
      相关资源
      最近更新 更多