SELECT count(womid) womnum,count(manid) mannum, t.dishi,t.xianqu,t.jiedao,sum(t.a) as a,sum(t.b) as b,sum(diagnosis11) as diagnosis11,sum(diagnosis12) as diagnosis12,
sum(diagnosis21) as diagnosis21,sum(diagnosis22) as diagnosis22,sum(diagnosis31) as diagnosis31,sum(diagnosis32) as diagnosis32
,sum(diagnosis41) as diagnosis41,sum(diagnosis42) as diagnosis42,sum(diagnosis51) as diagnosis51,sum(diagnosis52) as diagnosis52
,sum(diagnosis61) as diagnosis61,sum(diagnosis62) as diagnosis62,sum(medicine_opinion11) as medicine_opinion11,sum(medicine_opinion12) as medicine_opinion12
,sum(medicine_opinion21) as medicine_opinion21,sum(medicine_opinion22) as medicine_opinion22
,sum(medicine_opinion31) as medicine_opinion31,sum(medicine_opinion32) as medicine_opinion32
,sum(medicine_opinion41) as medicine_opinion41,sum(medicine_opinion42) as medicine_opinion42,
sum(al) as al,sum(bl) as bl
from
(SELECT
getRegionTextByCode(p_wom.xz_region_code,1,4) AS dishi,
getRegionTextByCode(p_wom.xz_region_code,1,6) AS xianqu,
getRegionTextByCode(p_wom.xz_region_code,1,9) AS jiedao,
if(p_wom.`name` is NULL,0,1)a,-- 婚查人数
if(p_man.`name` is NULL,0,1)b,-- 婚查人数
f.id womid,
f2.id manid,
CASE WHEN f.diagnosis LIKE '%严重遗传性疾病%' THEN '1' ELSE '' END as 'diagnosis11',
CASE WHEN f2.diagnosis LIKE '%严重遗传性疾病%' THEN '1' ELSE '' END as 'diagnosis12',
CASE WHEN f.diagnosis LIKE '%有关精神病%' THEN '1' ELSE '' END as 'diagnosis21',
CASE WHEN f2.diagnosis LIKE '%有关精神病%' THEN '1' ELSE '' END as 'diagnosis22',
CASE WHEN f.diagnosis LIKE '%生殖系统疾病%' THEN '1' ELSE '' END as 'diagnosis31',
CASE WHEN f2.diagnosis LIKE '%生殖系统疾病%' THEN '1' ELSE '' END as 'diagnosis32',
CASE WHEN f.diagnosis LIKE '%内科系统疾病%' THEN '1' ELSE '' END as 'diagnosis41',
CASE WHEN f2.diagnosis LIKE '%内科系统疾病%' THEN '1' ELSE '' END as 'diagnosis42',
CASE WHEN f.diagnosis LIKE '%传染病%' THEN '1' ELSE '' END as 'diagnosis51',
CASE WHEN f2.diagnosis LIKE '%传染病%' THEN '1' ELSE '' END as 'diagnosis52',
CASE WHEN f.diagnosis LIKE '%性病%' THEN '1' ELSE '' END as 'diagnosis61',
CASE WHEN f2.diagnosis LIKE '%性病%' THEN '1' ELSE '' END as 'diagnosis62',
if((f.diagnosis LIKE '%严重遗传性疾病%' or f.diagnosis LIKE '%有关精神病%' or f.diagnosis LIKE '%生殖系统疾病%'
or f.diagnosis LIKE '%内科系统疾病%' or f.diagnosis LIKE '%传染病%' or f.diagnosis LIKE '%性病%'),'1','0') as al,
if((f2.diagnosis LIKE '%严重遗传性疾病%' or f2.diagnosis LIKE '%有关精神病%' or f2.diagnosis LIKE '%生殖系统疾病%'
or f2.diagnosis LIKE '%内科系统疾病%' or f2.diagnosis LIKE '%传染病%' or f2.diagnosis LIKE '%性病%'),'1','0') as bl,
CASE WHEN f.medicine_opinion = '3' THEN '1' ELSE '' END as 'medicine_opinion11',
CASE WHEN f2.medicine_opinion = '3' THEN '1' ELSE '' END as 'medicine_opinion12',
CASE WHEN f.medicine_opinion = '4' THEN '1' ELSE '' END as 'medicine_opinion21',
CASE WHEN f2.medicine_opinion = '4' THEN '1' ELSE '' END as 'medicine_opinion22',
CASE WHEN f.medicine_opinion = '2' THEN '1' ELSE '' END as 'medicine_opinion31',
CASE WHEN f2.medicine_opinion = '2' THEN '1' ELSE '' END as 'medicine_opinion32',
CASE WHEN f.medicine_opinion = '5' THEN '1' ELSE '' END as 'medicine_opinion41',
CASE WHEN f2.medicine_opinion = '5' THEN '1' ELSE '' END as 'medicine_opinion42',
hh.region_code
FROM t_pre_checkups_registr pcr
LEFT JOIN t_hos_hospital hh on hh.id=pcr.hospital_id
LEFT JOIN t_arc_person p_wom on (p_wom.id=pcr.wom_base_msg and p_wom.sex_code=2)
LEFT JOIN t_arc_person p_man on (p_man.id=pcr.man_base_msg and p_man.sex_code=1)
left join t_pre_result f on(p_wom.id = f.arc_id and f.statue=0)
left join t_pre_result f2 on(p_man.id = f2.arc_id and f2.statue=0)
where pcr.statue=0
${pre_checkups_jcrq_start_where}
${pre_checkups_jcrq_end_where}
${manageUnitId_where}
${manageUnitRegionCode_where}
)t
GROUP BY t.dishi,t.xianqu,t.jiedao
order by t.region_code
1.group by +多字段 分组:select 后面的字段必须是分组字段 即group by 后面的字段;还可以跟聚合函数
2.注意left join+表 on后面 条件的位置谨慎放where后面;
3.count(字段),字段若为空,则不计数;