如何在PostgreSQL中创建命名窗口分区(别名)? [英] How do I create named window partitions (aliases) in PostgreSQL?

查看:100
本文介绍了如何在PostgreSQL中创建命名窗口分区(别名)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PostgreSQL窗口函数似乎暗示您可以在查询的多个位置使用相同的命名窗口。但是,我不知道如何创建命名窗口?

The documentation for PostgreSQL window functions seems to imply you can use the same named window in multiple places in your query. However, I can't figure out how do I create a named window?

SELECT first_value(vin) OVER( PARTITION BY vin ) AS w, first_value(make) OVER w
FROM inventory.vehicles
WHERE lot_Id = 9999 AND make is not null;

这是一个玩笑的查询,我正在尝试采用语法,但我得到了错误:

This is a joke query I'm trying to get the syntax to take, but I'm getting error:


错误:窗口 w不存在

ERROR: window "w" does not exist


推荐答案

答案实际上是在 SELECT doc:

The answer was actually in the SELECT doc:


WINDOW 条款

WINDOW Clause

可选的 WINDOW 子句的格式为

The optional WINDOW clause has the general form

WINDOW window_name AS(window_definition)[,...]

这里是一个例子,

SELECT first_value(vin) OVER w,
  first_value(make) OVER w
FROM inventory.vehicles
WHERE lot_Id = 9999
  AND make is not null
WINDOW w AS ( PARTITION by vin );

这篇关于如何在PostgreSQL中创建命名窗口分区(别名)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆