在 mysql 中使用相同的 table 拆分多个计数

Split multiple count using in mysql with same table

我有以下格式的查询:

SELECT ETM.etm_Taxonomy, COUNT( PE.pp_profileID ) AS total_counts
FROM expertise_taxonomymaster AS ETM
LEFT JOIN expertise_taxonomy AS ET ON ETM.etm_ID = ET.`et_Taxonomy`
LEFT JOIN expertise AS E ON E.et_Taxonomy = ET.`et_ID`
LEFT JOIN profile_expertise AS PE ON PE.pp_expertiseID = E.et_ID
WHERE PE.pp_profileID IN (    
        SELECT PJ.pj_profileID
        FROM jobtitle_taxonomymaster AS JTM
        LEFT JOIN jobtitle_taxonomy AS JT ON JTM.jtm_ID = JT.`jt_Taxonomy`
        LEFT JOIN jobtitle AS J ON J.jt_taxonomy = JT.`jt_ID`
        LEFT JOIN profile_jobtitle AS PJ ON PJ.pj_jobtitleID = J.jt_ID
        WHERE JTM.jtm_Taxonomy = 'Associate'
            OR JTM.jtm_Taxonomy = 'Partner' 
        )
    AND et_lawfirmID in (195,196)
GROUP BY etm_Taxonomy

我得到的结果如下:

etm_Taxonomy    total_counts
Advertising       18
Antitrust         47    
Banking          258    

但我需要以下结果,Count 应该根据 JTM.ttm_Taxonomy 字段进行拆分

etm_Taxonomy    Patners195  Partners196     Associates195   Associates196
Advertising       18          18              18              18
Antitrust         47          47              47              47
Banking          258          258            258              258  

试试这个方法:

 SELECT ETM.etm_Taxonomy, 
   SUM (CASE WHEN PJ_TAX.jtm_Taxonomy = 'Associate' THEN 1 ELSE 0 END) AS Associates,
   SUM (CASE WHEN PJ_TAX.jtm_Taxonomy = 'Partner' THEN 1 ELSE 0 END) AS Partners,
   SUM (CASE WHEN PJ_TAX.jtm_Taxonomy = 'Consultant' THEN 1 ELSE 0 END) AS Consultants,
   SUM (CASE WHEN PJ_TAX.jtm_Taxonomy = 'Counsel' THEN 1 ELSE 0 END) AS Counsels,
 COUNT(PE.pp_profileID ) AS total_counts
FROM expertise_taxonomymaster AS ETM
LEFT JOIN expertise_taxonomy AS ET ON ETM.etm_ID = ET.`et_Taxonomy`
LEFT JOIN expertise AS E ON E.et_Taxonomy = ET.`et_ID`
LEFT JOIN profile_expertise AS PE ON PE.pp_expertiseID = E.et_ID
INNER JOIN
 (
SELECT DISTINCT PJ.pj_profileID,JTM.jtm_Taxonomy
FROM jobtitle_taxonomymaster AS JTM
LEFT JOIN jobtitle_taxonomy AS JT ON JTM.jtm_ID = JT.`jt_Taxonomy`
LEFT JOIN jobtitle AS J ON J.jt_taxonomy = JT.`jt_ID`
LEFT JOIN profile_jobtitle AS PJ ON PJ.pj_jobtitleID = J.jt_ID
WHERE JTM.jtm_Taxonomy = 'Associate'
OR JTM.jtm_Taxonomy = 'Partner'
OR JTM.jtm_Taxonomy = 'Consultant'
OR JTM.jtm_Taxonomy = 'Counsel'
) as PJ_TAX
ON  PE.pp_profileID= PJ_TAX.pj_profileID
WHERE et_lawfirmID =195
GROUP BY etm_Taxonomy

首先:您的左外部联接实际上不是外部联接,因为在您的 WHERE 子句中您说您只需要某些 ET 和 PE。

主要是你什么都想加入,然后看是partner还是associate,是195还是196,照着算。这可以通过 COUNT 中的 CASE 结构来完成。唯一的问题可能是重复导致计数不正确。我不完全确定您的数据库结构。如果您的内部查询可能有重复的 profileID,您需要一个具有不同的派生查询,而不是直接连接所有内容。检查这是否适合您:

select 
  etm.etm_taxonomy, 
  count(case when t.jtm_taxonomy = 'Partner' and et_lawfirmid = 195 then 1 end) as patners195,
  count(case when t.jtm_taxonomy = 'Partner' and et_lawfirmid = 196 then 1 end) as patners196,
  count(case when t.jtm_taxonomy = 'Associate' and et_lawfirmid = 195 then 1 end) as associates195,
  count(case when t.jtm_taxonomy = 'Associate' and et_lawfirmid = 196 then 1 end) as associates196
from expertise_taxonomymaster as etm
join expertise_taxonomy as et on etm.etm_id = et.et_taxonomy
join expertise as e on e.et_taxonomy = et.et_id
join profile_expertise as pe on pe.pp_expertiseid = e.et_id
join
(
  select distinct pj.pj_profileid, jtm.jtm_taxonomy
  from jobtitle_taxonomymaster as jtm
  join jobtitle_taxonomy as jt on jtm.jtm_id = jt.jt_taxonomy
  join jobtitle as j on j.jt_taxonomy = jt.jt_id
  join profile_jobtitle as pj on pj.pj_jobtitleid = j.jt_id
  where jtm.jtm_taxonomy in ('Associate', 'Partner')
) as t on t.pj_profileid = pe.pp_profileid
where et.et_lawfirmid in (195,196);
group by etm.etm_taxonomy;