我执行此查询
SELECT Kd_penyakit, Penyakit,
COUNT(case when Kd_gejala in ('G01','','G03','G04','','','','','','','','','','','','','','','','','','')then 1 else null end) / COUNT(kd_penyakit)*100 AS cases
FROM tbl_casebase
GROUP BY Kd_penyakit
ORDER BY cases DESC
结果是这样的
Kd_penyakit Penyakit cases
P01 Tipus Ayam 42.8571
P06 Produksi Telur 25.0000
P03 Salesma Ayam 14.2857
P02 Berak Darah 0.0000
P04 Gumboro 0.0000
P05 Mareks 0.0000
我的问题是如何排除0值,所以结果就像这样
Kd_penyakit Penyakit cases
P01 Tipus Ayam 42.8571
P06 Produksi Telur 25.0000
P03 Salesma Ayam 14.2857
最佳答案
使用exists
过滤聚合之前不需要的行:
SELECT Kd_penyakit, Penyakit,
COUNT(case when Kd_gejala in ('G01','G03','G04','') then 1 end) / COUNT(kd_penyakit)*100 AS cases
FROM tbl_casebase
where exists (
select 1 from tbl_casebase t2
where tbl_casebase.Kd_penyakit = t2.Kd_penyakit
and t2.Kd_gejala in ('G01','G03','G04','')
)
GROUP BY Kd_penyakit
ORDER BY cases DESC
;
注意,当在
count()
中使用case表达式时,可以省略“ else null”关于mysql - SQL-排除0值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49020477/