Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在mysql中使用同一个表拆分多个计数_Mysql_Join_Left Join - Fatal编程技术网

在mysql中使用同一个表拆分多个计数

在mysql中使用同一个表拆分多个计数,mysql,join,left-join,Mysql,Join,Left Join,我有一个以下格式的查询: 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 p

我有一个以下格式的查询:

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    
但我需要下面的结果,计数应该根据
JTM.ttm_分类法
字段进行分割

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子句中,您说您只需要某些ETs和PEs

主要是要加入所有内容,然后查看是合作伙伴还是合作伙伴,以及是195还是196,并相应地计算。这可以通过内部计数的CASE构造来完成。唯一的问题可能是导致不正确计数的重复。我不完全确定你的数据库结构。如果您的内部查询中可能存在重复的profileid,那么您需要一个具有distinct的派生查询,而不是直接连接所有内容。检查这是否适用于您:

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;

谢谢@alex,这很有效。但我需要为pj_profileID提供清晰的信息。在哪里可以添加DISTINCT?我认为DISTINCT不起作用。它还显示计数器中的重复项。如果没有SLQFIDLE,调试非常困难。你能提供一些吗?抱歉@alex,它有大数据和多个连接,所以请留下这个区别。我还有一个疑问。如果假设我会给出类似的
其中et_lawfirmID在(195196)
。在这种情况下,我需要显示每个et_lawfirmID的计数。是否有可能使用新的额外查询结果和所需的预期结果更新您的原始帖子?谢谢@thorsten。我还需要补充一点,请告诉我这是有意义的<代码>在e.et_taxonomy=et.et_id上以e的形式加入专业知识在这行中,我可以添加或设置
将专业知识作为e的形式加入(e.et_taxonomy=et.et_id)或(e.et_taxonomy1=et.et_id)或(e.et_taxonomy2=et.et_id)
这是可能的,但您的总数当然可能会更高,因为您不仅要为et_taxonomonomonomonomonomonomy添加所有的配置文件,也适用于et_分类1和2。这个新的ON子句也可以写成
在(e.et_分类法,e.et_分类法1,e.et_分类法2)
中作为e ON et.et_id加入专业知识。