以下是下面的查詢。請建議我如何簡化它:從單個查詢中的相同表中獲取不同組狀態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>
將您的表格結構以 –
表格結構與您當前的活動記錄查詢結合起來 –
@MayankPandeyz:請參考下面的表格結構圖。在此先感謝 –