2016-12-15 55 views
0

我想根據多個表中的ID顯示計數。對於兩個表是工作正常,但對於三桌它不顯示數據如何使用連接查詢從多個表中獲取計數(id)

這是我三個表的查詢它不工作

select r.req_id 
    , r.no_of_positions 
    , count(j.cand_id) as no_of_closure 
    , count(cis.cand_id) 
from requirement r 
join joined_candidates j 
    on r.req_id=j.req_id 
join candidate_interview_schedule cis 
    on cis.req_id=r.req_id 
where cis.interview_status='Interview Scheduled' 
group by r.req_id, r.no_of_positions; 
+0

沒有結果意味着兩件事中的一件事:1 Where子句條件是排除所有數據。正在進行連接,並且未找到加入標準上的匹配項。我們沒有足夠的信息來了解問題所在。 where子句中的區分大小寫?消除where子句,你會得到計數/結果嗎?如果是這樣,上面兩塊和修剪? 'TRIM(UPPER(cis.interview_status))= UPPER('Interview Scheduled')'對於連接:你確定你有一個req_ID存在於所有3個表中嗎?我們不知道兩個表的工作原理。這將有助於隔離問題。 – xQbert

+0

req_id在不同的表中有所不同,但我想顯示所有req_id,如果值不匹配其他表中的值,則返回null –

+0

然後,您需要或許需要在兩個表上使用LEFT Join,以便如果req_ID不會不存在於兩個表中的一箇中,它仍將被顯示。此外,如果你的這些表有1:M relatioships,那麼你的計數將會被誇大,所以你可能需要使用分區計數... – xQbert

回答

0
  1. 改爲左聯接櫃面值好好嘗試一下存在在表
  2. 改變計數使用一個窗口功能,因此計數沒有人爲誇大的加入
  3. 移動where子句的加入標準作爲一個LEFT JOIN,那就否定了空值,使其像一個內部聯接操作。

..MAYBE ...

SELECt r.req_id 
    , r.no_of_positions 
    , count(j.cand_id) over (partition by J.cand_ID) as no_of_closure 
    , count(cis.cand_id) over (partition by cis.cand_id) as no_of_CIS_CNT 
FROM requirement r 
LEFT join joined_candidates j 
    on r.req_id=j.req_id 
LEFT join candidate_interview_schedule cis 
    on cis.req_id=r.req_id 
and cis.interview_status='Interview Scheduled' 
GROUP BY r.req_id, r.no_of_positions; 

或許...(如果我可以假設j.cand_ID和cis.cand_ID是唯一的)也以消除由於1仿真數增加:M加入

SELECt r.req_id 
    , r.no_of_positions 
    , count(distinct j.cand_id) as no_of_closure 
    , count(distinct cis.cand_id) as no_of_CIS_CNT 
FROM requirement r 
LEFT join joined_candidates j 
    on r.req_id=j.req_id 
LEFT join candidate_interview_schedule cis 
    on cis.req_id=r.req_id 
and cis.interview_status='Interview Scheduled' 
GROUP BY r.req_id, r.no_of_positions; 
+0

是的,它解決了我的問題謝謝 –

相關問題