有人能指导我如何重写这个查询吗。
如果country='i',则我需要获取状态计数,否则需要根据id Column分配为0。
SELECT id,case when country = 'I' then count(state) else 0 end as state_cnt
FROM info_cntry group by id;
但它的抛出错误(如count(尚未得到支持。
这就是你想要的吗?
select id,
count(case when country = 'I' then state end) as state_cnt
from info_cntry
group by id;
另一方面,如果每个id
只有一个country
,那么只需将其添加到原始查询的group by
子句中即可:
select id,
case when country = 'I' then count(state) else 0 end as state_cnt
from info_cntry
group by id, country