postgresql - postgresql while/循环函数
问题描述
有没有办法用 while/loop 函数替换/简化以下代码?我需要在项目<特定数字或变量时循环它。谢谢!
select
count(distinct wallet) filter (where items >= 3 and items < 6) +
count(distinct wallet) filter (where items >= 6 and items < 9) +
count(distinct wallet) filter (where items >= 9 and items < 12) +
count(distinct wallet) filter (where items >= 12 and items < 15) as total_items
from
agg
where items > 0
解决方案
不,没有办法循环这个。(嗯,有,但它过于复杂并且可能效率低下)。
但是,您可以使用数学将items
每组的规范元素减少到一个,然后计算由和各自的规范元素组成的不同元组:wallet
select count(distinct (wallet, items/3)) as total_items
from agg
where items >= 3 and items < 15
推荐阅读
- json - Aps.net 核心 Razor 页面 [FromBody] Ajax 帖子模型始终为空
- mule-esb - 为什么 ESB 应用程序在部署期间尝试获取“'mule-tracking-ee.xsd'”时会生成警告?
- google-apps-script - Google API 返回响应 200 空 JSON
- html - 如何将搜索过滤器添加到角度的选择选项
- vue.js - 在 HTTPS 中运行 Visual Studio 默认的 Vue 项目
- javascript - 从数据属性中获取值到数组中
- c - C中实现mac地址表项老化的数据结构
- pandas - 使用 Airflow 进行多步 Pandas 处理
- java - Goal Seek 在 groovy 中的实现
- python - 练习的数据结构建议