0

我有两个疑问。一:

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(未成年人)。我想基本上合并这些查询,但联合函数在我的查询构建器中对我不起作用。关于如何改进这两个查询的结构并将未成年人和专业的所有数据返回到一个表中的任何建议,该表将输出:

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

2 回答 2

1

您需要使用外部联接。

有关详细信息,请参阅此文档

尝试这样的事情:

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);

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

于 2013-07-22T03:32:33.907 回答
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
于 2013-07-22T06:32:33.967 回答