2013-07-22 106 views
0

我有兩個查詢。一:聯合兩個查詢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在第一個查詢中(未成年人)。我想基本上結合這些查詢,但聯合函數不在我的查詢生成器中工作。關於如何提高這兩個查詢的結構和所有未成年人和專業的數據恢復到一個表的任何建議將輸出:

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

「whyn我嘗試加入這兩個「......怎麼樣?請顯示組合查詢。此外,你正在使用什麼「查詢構建器」? –

+1

「聯盟」與您所描述的不同。 – pstanton

回答

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

,在實踐中,你會爲你的子查詢或整理此查詢的其他方式的圖...

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