2017-10-05 38 views
0

以下是下面的查詢。請建議我如何簡化它:從單個查詢中的相同表中獲取不同組狀態ID的計數

在查詢中,我必須選擇具有指定提交狀態ID的電子郵件世代的計數。正如你所看到的,我必須從同一個表中提取計數,但我仍然需要爲不同的提交狀態集提供單獨的查詢。

SELECT a.id, a.created_at, a.recruiter_id, 
      CONCAT_WS(' ', first_name, middle_name, last_name) as f_name, 

(select count(*) from email_generations where email_generations.candidate_id = a.id and 
email_generations.submission_status_id = 2) as subm_count, 

SUM((select count(*) from submission_statuses where submission_statuses.id IN (7,8) and submission_statuses.id = 
email_generations.submission_status_id)) as phn_intw_count, 

SUM((select count(*) from submission_statuses where submission_statuses.id IN (9,10) and submission_statuses.id = 
email_generations.submission_status_id)) as intw_count, 

SUM((select count(*) from submission_statuses where submission_statuses.id IN (12,13) and submission_statuses.id = 
email_generations.submission_status_id)) as offer_count, 

SUM((select count(*) from submission_statuses where submission_statuses.id IN (11) and submission_statuses.id = 
email_generations.submission_status_id)) as ref_chk_count 

FROM candidates AS a 

INNER JOIN email_generations ON email_generations.candidate_id = a.id WHERE a.deleted_at IS NULL 

GROUP BY a.id HAVING a.recruiter_id = 1 and (subm_count <> 0 OR phn_intw_count <> 0 OR intw_count <> 0 OR offer_count <> 0 OR 
ref_chk_count <> 0) 

表:Email Generation Table Structure, Submission Statuses Table Structure

**所需的輸出應該像下面的例子 - **

<table border="0" cellspacing="0"> 
 
\t <colgroup span="2" width="85"> 
 
\t </colgroup> 
 
\t <colgroup width="174"> 
 
\t </colgroup> 
 
\t <colgroup width="85"> 
 
\t </colgroup> 
 
\t <colgroup width="110"> 
 
\t </colgroup> 
 
\t <colgroup span="2" width="85"> 
 
\t </colgroup> 
 
\t <colgroup width="92"> 
 
\t </colgroup> 
 
\t <tbody> 
 
\t \t <tr> 
 
\t \t \t <td align="left" height="17"><b>Candidate ID</b></td> 
 
\t \t \t <td align="left"><b>Recruiter ID</b></td> 
 
\t \t \t <td align="left"><b>Candidate Name</b></td> 
 
\t \t \t <td align="left"><b>subm_count</b></td> 
 
\t \t \t <td align="left"><b>phn_intw_count</b></td> 
 
\t \t \t <td align="left"><b>intw_count</b></td> 
 
\t \t \t <td align="left"><b>offer_count</b></td> 
 
\t \t \t <td align="left"><b>ref_chk_count</b></td> 
 
\t \t </tr> 
 
\t \t <tr> 
 
\t \t \t <td align="right" height="17" sdnum="16393;" sdval="258">11258</td> 
 
\t \t \t <td align="right" sdnum="16393;" sdval="1">1</td> 
 
\t \t \t <td align="left">Test Candidate</td> 
 
\t \t \t <td align="right" sdnum="16393;" sdval="0">4</td> 
 
\t \t \t <td align="right" sdnum="16393;" sdval="0">2</td> 
 
\t \t \t <td align="right" sdnum="16393;" sdval="0">9</td> 
 
\t \t \t <td align="right" sdnum="16393;" sdval="1">1</td> 
 
\t \t \t <td align="right" sdnum="16393;" sdval="0">3</td> 
 
\t \t </tr> 
 
\t </tbody> 
 
</table>

+0

將您的表格結構以 –

+1

表格結構與您當前的活動記錄查詢結合起來 –

+0

@MayankPandeyz:請參考下面的表格結構圖。在此先感謝 –

回答

0

感謝朋友們的美好時光。我自己優化了查詢,並希望與大家分享。

感謝@ user3366016提示從電子郵件生成中取出記錄而不是候選。

SELECT DISTINCT candidates.id,candidates.created_at, candidates.recruiter_id,CONCAT_WS(」」,candidates.first_name, candidates.middle_name,candidates.last_name)作爲f_name,

(SELECT COUNT( *)FROM email_generations AS e其中e.submission_status_id = 2和e.candidate_id = candidates.id)AS subm_count,

(SELECT COUNT(*)FROM email_generations AS e其中e.submission_status_id IN(7,8)和e.candidate_id = candidates.id)AS phn_intw_count,

(SELECT COUNT(*)FROM email_generations AS e其中e.submission_status_id IN(9,10)和e.candidate_id = candidates.id) AS intw_count,

(SELECT COUNT(*)FROM email_generations AS e其中e.submission_status_id IN(12,13)和e.candidate_id = candidates.id) AS offer_count,

(SELECT COUNT(*)FROM email_generations AS e其中e.submission_status_id IN(11)和e.candidate_id = candidates.id)AS ref_chk_count

FROM email_genera蒸發散

INNER JOIN候選ON candidates.id = email_generations.candidate_id AND WHERE email_generations.deleted_at IS NULL AND(candidates.recruiter_id = 1和 email_generations candidates.deleted_at IS NULL

。submission_status_id IN(2,7,8,9,10,12,13,11))

HAVING(subm_count <> 0 OR phn_intw_count <> 0 OR intw_count <> 0 OR offer_count <> 0 OR ref_chk_count <> 0 )

相關問題