使用 SQL 同时使用计数和大小写

Use count and case together using SQL

我想查询return 3列,有多少A型血患者是患者集, 有多少B型血患者,根据患者有多少国家。

每位患者都使用唯一 ID 进行识别,因此 patientID 是我正在做的事情。每个州仅使用州缩写和血型字母。

还有患者集合,集合只是一组患者集合在一起,所以就像一堆患者 ID 一样,它们也像患者 ID 一样是唯一的

到目前为止,我有类似的东西,我不想使用 SUM,因为这会将每个 patientsID 号加在一起,我应该使用 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

您可以将 sumcase expression 一起使用。你不需要 distinctgroup 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