我有以下查询:
SELECT apdr.patient_id,
COUNT(apdr.patient_id)
AS x_ray_result
FROM apdr
WHERE apdr.type_of_test IN ('x_ray_opinion_1', 'x_ray_opinion_2')
AND apdr.interpretation LIKE 'kochs'
GROUP BY apdr.patient_id
它返回解释为'kochs'的患者数量的计数。如何通过输出中的相应patient_id修改此计数以使计数为0。
感谢任何帮助。
我从1开始计数,我需要查询,我也得到0作为计数。
答案 0 :(得分:2)
您可以使用case语句返回条件计数,而不是将条件放在where子句中。参见:
SELECT apdr.patient_id,
COUNT(CASE WHEN apdr.type_of_test IN ('x_ray_opinion_1', 'x_ray_opinion_2')
AND apdr.interpretation LIKE 'kochs'
THEN adpr.patient_id
END CASE) AS x_ray_result
FROM apdr
GROUP BY apdr.patient_id
答案 1 :(得分:1)
尝试
SELECT a1.patient_id,
(SELECT COUNT(*) FROM apdr a2
WHERE a2.interpretation LIKE 'kochs'
AND a1.patient_id=a2.patient_id) AS x_ray_result
FROM apdr a1
WHERE a1.type_of_test IN ('x_ray_opinion_1', 'x_ray_opinion_2')
GROUP BY a1.patient_id
编辑:我忘记了WHERE
条款
答案 2 :(得分:0)
选择不同的a.patient_id,if(b.x_ray_result为null,0,b.x_ray_result)为来自apdr的x_ray_result左连接(SELECT patient_id,COUNT(patient_id)AS x_ray_result FROM apdr WHERE type_of_test IN('x_ray_opinion_1', 'x_ray_opinion_2')AND解释LIKE'kochs'GROUP BY patient_id)b on a.patient_id = b.patient_id;