如何解决使用 SQL 一起使用计数和大小写
我想用一个查询返回3列,有多少A型血患者是患者集, 那里有多少 B 型血患者,根据患者有多少个国家。
每个患者都使用唯一的 ID 进行标识,因此患者 ID 是我所依赖的。每个州只使用州的缩写,血型只使用字母。
并且有患者集合,集合只是将患者集合在一起,因此就像一堆患者 ID 一样,它们也像患者 ID 一样是唯一的
到目前为止,我有这样的东西,我不想使用 SUM,因为这会将每个患者 ID 号加在一起,我应该使用 Count。有没有办法使用案例场景来计算?或者有没有更好的方法来完成我想要的?
select distinct PTID,select count (patientID CASE WHEN bloodtype = 'A') as totalAbloodtype,select count (patientID CASE WHEN bloodtype = 'AB') as totalABbloodtype,select count (distinct countrycode) as totalcountriesinset
from patientsinfo
and PTID is not null
group by PTID
解决方法
您可以将 sum
与 case expression
一起使用。您不需要 distinct
和 group by
,而且您似乎缺少 where
和大量的 selects
,因此您拥有的代码只是语法错误。>
显然没有样本数据或想要的结果我无法测试,但想法是
select PTID,sum (CASE WHEN bloodtype = 'A' then 1 else 0 end) as totalAbloodtype,sum (CASE WHEN bloodtype = 'AB' then 1 else 0 end) as totalABbloodtype,count (distinct countrycode) as totalcountriesinset
from patientsinfo
where PTID is not null
group by PTID
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。