联盟两个查询PostgreSQL

时间:2013-07-22 03:26:29

标签: postgresql

我有两个问题。之一:

select driver_id,count(*) as minors from zendesk_tickets
where city = 'Los Angeles'
and (tags like '%la_driver_quality_inefficientroute%' 
or tags like '%la_driver_quality_attitude%'
or tags like '%la_driver_quality_dirtycar%'
or tags like '%la_driver_quality_hygiene%'
or tags like '%la_driver_quality_wronguser%'
or tags like '%la_driver_quality_starttripearly%'
or tags like '%la_driver_quality_arrivingearly%'
or tags like '%la_driver_quality_didnotanswer%'
or tags like '%la_driver_quality_nodoor%'
or tags like '%la_driver_quality_cigarettes%'
or tags like '%la_driver_quality_startstop%'
or tags like '%la_driver_quality_arguing%'
or tags like '%la_driver_quality_refusedestination%'
or tags like '%la_driver_quality_onphoneontrip%'
)
and driver_id is not null
group by driver_id
order by minors desc

哪个输出:

 driver_id     minors
1234           2
1243           3
etc           etc

另一个疑问:

select driver_id,count(*) as majors from zendesk_tickets
where city = 'Los Angeles'
and (tags like '%la_driver_quality_acceptcash%'
or tags like '%la_driver_quality_solicitingclient%'
or tags like '%la_driver_quality_majordisrespect%'
or tags like '%la_driver_quality_physicalaltercation%'
or tags like '%la_driver_quality_sexualharassment%'
or tags like '%la_driver_quality_majordrivingviolation%'
or tags like '%la_driver_quality_dui%'
or tags like '%la_driver_quality_usingaccount%'
or tags like '%la_driver_quality_expiredinfo%'
)
and driver_id is not null
group by driver_id
order by majors desc

哪个输出:

driver_id     majors
1234           1
1243           2
etc           etc

当我尝试连接这两个(左连接)时,它仍然只返回第一个查询(未成年人)中的驱动程序ID。我想基本上联合这些查询,但在我的querybuilder中,union函数不适用于我。关于如何改进这两个查询的结构并将未成年人和专业的所有数据返回到一个表中的任何建议,这些表将输出:

driver_id     minors      majors
1234           1           3
1243           2           4
etc           etc          etc.

2 个答案:

答案 0 :(得分:1)

您需要使用外部联接。

请参阅this document for details

尝试这样的事情:

select min.driver_id, majors, minors from (

    select driver_id,count(*) as minors from zendesk_tickets
    where city = 'Los Angeles'
    and (tags like '%la_driver_quality_inefficientroute%' 
    or tags like '%la_driver_quality_attitude%'
    or tags like '%la_driver_quality_dirtycar%'
    or tags like '%la_driver_quality_hygiene%'
    or tags like '%la_driver_quality_wronguser%'
    or tags like '%la_driver_quality_starttripearly%'
    or tags like '%la_driver_quality_arrivingearly%'
    or tags like '%la_driver_quality_didnotanswer%'
    or tags like '%la_driver_quality_nodoor%'
    or tags like '%la_driver_quality_cigarettes%'
    or tags like '%la_driver_quality_startstop%'
    or tags like '%la_driver_quality_arguing%'
    or tags like '%la_driver_quality_refusedestination%'
    or tags like '%la_driver_quality_onphoneontrip%'
    )
    and driver_id is not null
    group by driver_id
    order by minors desc

) as min left outer join  (

    select driver_id,count(*) as majors from zendesk_tickets
    where city = 'Los Angeles'
    and (tags like '%la_driver_quality_acceptcash%'
    or tags like '%la_driver_quality_solicitingclient%'
    or tags like '%la_driver_quality_majordisrespect%'
    or tags like '%la_driver_quality_physicalaltercation%'
    or tags like '%la_driver_quality_sexualharassment%'
    or tags like '%la_driver_quality_majordrivingviolation%'
    or tags like '%la_driver_quality_dui%'
    or tags like '%la_driver_quality_usingaccount%'
    or tags like '%la_driver_quality_expiredinfo%'
    )
    and driver_id is not null
    group by driver_id
    order by majors desc

) as maj on (min.driver_id=maj.driver_id);

当然,在实践中,您将为子查询或其他一些整理此查询的方式创建一个视图......

答案 1 :(得分:0)

select data.driver_id, sum(data.minor), sum(data.major)
from (
    select driver_id,
        case when 
            tags like '%la_driver_quality_inefficientroute%' 
            or tags like '%la_driver_quality_attitude%'
            or tags like '%la_driver_quality_dirtycar%'
            or tags like '%la_driver_quality_hygiene%'
            or tags like '%la_driver_quality_wronguser%'
            or tags like '%la_driver_quality_starttripearly%'
            or tags like '%la_driver_quality_arrivingearly%'
            or tags like '%la_driver_quality_didnotanswer%'
            or tags like '%la_driver_quality_nodoor%'
            or tags like '%la_driver_quality_cigarettes%'
            or tags like '%la_driver_quality_startstop%'
            or tags like '%la_driver_quality_arguing%'
            or tags like '%la_driver_quality_refusedestination%'
            or tags like '%la_driver_quality_onphoneontrip%' then 1 else 0 end minor,
        case when 
            tags like '%la_driver_quality_acceptcash%'
            or tags like '%la_driver_quality_solicitingclient%'
            or tags like '%la_driver_quality_majordisrespect%'
            or tags like '%la_driver_quality_physicalaltercation%'
            or tags like '%la_driver_quality_sexualharassment%'
            or tags like '%la_driver_quality_majordrivingviolation%'
            or tags like '%la_driver_quality_dui%'
            or tags like '%la_driver_quality_usingaccount%'
            or tags like '%la_driver_quality_expiredinfo%'then 1 else 0 end major
    from zendesk_tickets
    where city = 'Los Angeles'
    and driver_id is not null) data
group by data.driver_id