sql - postgresql 获取多个值的唯一计数
问题描述
我是 POSTGRESQL 的新手,正在尝试获取deleted_by is null
多个comp_source_store
值的唯一计数:[“petsmart”、“amazon”、“qfc”]
我可以通过一个查询得到我的号码,如下所示:
select
count(distinct(comp_upc)) as petsmart from matches where
comp_source_store='petsmart'
and deleted_by is null
返回如下响应:
productMatches = [ { petsmart: '11562' } ]
我试图最终让我的查询返回一个带有 int 值的响应,comp_source_store
如下所示:
productMatches = [ { petsmart: '11562' }, { amazon: '231' }, { qfc: '5333' }, ]
我试图用逗号分隔我的查询以获得两个唯一计数,但查询不会运行并导致指向逗号字符的错误:
select
count(distinct(comp_upc)) as petsmart from matches where
comp_source_store='petsmart' and deleted_by is null,
count(distinct(comp_upc)) as amazon from matches where
comp_source_store='amazon' and deleted_by is null
解决方案
您可以使用 group by 将每个商店放在一行中,如下所示:
select
comp_source_store,
count(distinct(comp_upc)) as comp_upc_count
from matches
where
comp_source_store in ('petsmart', 'amazon', 'qfc')
and deleted_by is null
group by
comp_source_store
推荐阅读
- bash - 以特定格式格式化日志
- python - 当时间数据已被结算为索引时,如何绘制时间相关数据?
- javascript - 如何处理来自“不透明”类型的获取响应?
- firebase - 使用 FutureBuilder 时异步加载两个依赖流
- oracle - 要在 BI 报表中显示的 Oracle Unifier 图像数据,图像数据只显示一个数字
- javascript - 使用 jquery 删除动态创建的行并绑定/更新现金总额
- android - RecyclerView 从后端 api 加载 10000+ 图像
- python - “查看”函数返回一个前向位置错误的视图矩阵?(Python 实现)
- networking - 如何在linux中配置网桥连接
- angular - 如何从包脚本启动和构建运行 gulp 命令