您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何在Postgres中使用别名创建嵌套的SELECT COUNT

如何在Postgres中使用别名创建嵌套的SELECT COUNT

您要为每个字符创建一个单独的行。一种方法生成所有字符,然后由它们聚合。这是一种方法

select chr(chars.c + ascii('A')) as c,
       sum(case when ascii(left(m.nome, 1)) = chars.c + ascii('A') then 1 else 0 end)
from generate_series(0, 25) as chars(c) cross join
     merchant m
group by c;

编辑:

艾伦的建议是一个更好的查询

select chr(chars.c + ascii('A')) as c,
       count(m.nome)
from generate_series(0, 25) as chars(c) left join
     merchant m
     on ascii(left(m.nome, 1)) = chars.c + ascii('A')
group by c;
Postgres 2022/1/1 18:46:43 有294人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶