postgresql - postgres查询中的多个命名窗口
问题描述
postgres 文档因此指定了一个窗口定义子句:
[ WINDOW window_name AS ( window_definition ) [, ...] ]
[ ,...]指定多个窗口是可能的。我在文档中找不到其他任何东西来确认或否认这是可能的。我该如何进行这项工作?
在此查询中,我可以单独使用任一窗口子句,但即使语法遵循规范,我也不能同时使用两者:
select q.*
, min(value) over w_id as min_id_val
--, min(value) over w_kind as min_kind_val
from (
select 1 as id, 1 as kind, 3.0 as value
union select 1, 2, 1.0
union select 2, 1, 2.0
union select 2, 2, 0.5
) as q
window w_id as (partition by id)
-- ,
-- window w_kind as (partition by kind)
我可以通过不使用窗口定义来获得技术效果,但是对于重用窗口的复杂查询来说,这会让人厌烦:
select q.*
, min(value) over (partition by id) as min_id_val
, min(value) over (partition by kind) as min_kind_val
from (
select 1 as id, 1 as kind, 3.0 as value
union select 1, 2, 1.0
union select 2, 1, 2.0
union select 2, 2, 0.5
) as q
解决方案
不要重复window
关键字:
select q.*,
min(value) over w_id as min_id_val,
min(value) over w_kind as min_kind_val
from (
values
(1,1,3.0),
(1, 2, 1.0),
(2, 1, 2.0),
(2, 2, 0.5)
) as q(id,kind,value)
window w_id as (partition by id),
w_kind as (partition by kind)
推荐阅读
- django - 使用父模型 id 查询相关模型
- reactjs - 无法读取 React 中未定义的“地图”属性 - 我很笨,我拼错了成分,谢谢大家帮助我
- python - 从列表中删除具有特定属性的对象的最快方法是什么
- flutter - 未来返回正确的数据,但快照显示为空
- docker - Docker 容器退出后,绑定安装文件夹中的文件似乎消失了
- python - 数据逐个字母而不是逐字进入 SQLite3 表
- jwt - 这个 JWT 实现是否可以防止 XSS 和 CSRF 攻击,同时仍然允许我访问有效负载?
- javascript - Javascript 对象方法上的“this”
- python - python datetime strptime错误ValueError:时间数据与linux ls输出格式的格式不匹配
- kubernetes - kubelet 停止发布节点状态和节点“k8sslave1”在 kubernetes 中找不到 kubelet