简体   繁体   English

在 Postgres 中使用连接指定窗口函数

[英]Specifying window functions with joins in Postgres

I get a syntax error with this SQL command in Postgres, but it seems like it matches the command syntax specified in the SELECT documentation我在 Postgres 中使用此 SQL 命令遇到语法错误,但它似乎与SELECT 文档中指定的命令语法相匹配

select *, 
       row_number() over w 
from units 
   left outer join leases 
window w AS (partition by units.id order by leases.start desc)

As far as I can see this matches the syntax which says I can write any SQL statement that is in the form of:就我所见,这与表示我可以编写以下形式的任何 SQL 语句的语法相匹配:

SELECT * 
from (table joins) 
WINDOW window_name AS (window_definition)

and yet SQL tells me there's an error between leases and window .然而 SQL 告诉我leaseswindow之间存在错误。 What's the error?有什么错误? Why am I misreading the documentation?为什么我误读了文档?

Here's the error:这是错误:

2020-03-27 23:02:11.644 PDT [47717] ERROR:  syntax error at or near "WINDOW" at character 66
2020-03-27 23:02:11.644 PDT [47717] STATEMENT:  select *, row_number() over w from  units left outer join leases WINDOW w AS (partition by units.id order by leases.start desc)
ERROR:  syntax error at or near "WINDOW"
LINE 1: ...umber() over w from  units left outer join leases WINDOW w A...
                                                             ^

To clarify my question, I edited it to actually add a window function to the SELECT list, though it's not necessary per the Postgresql SQL spec.为了澄清我的问题,我对其进行了编辑,以将窗口函数实际添加到 SELECT 列表中,尽管根据 Postgresql SQL 规范这不是必需的。 At any rate, adding the function doesn't take care of the syntax error;无论如何,添加函数并不能解决语法错误; it remains at the same spot.它仍然在同一地点。

As hwnn has commented your sql is not in error because of your use of WINDOW, it's because you haven't put any predicates for the join正如 hwnn 所评论的,由于您使用了 WINDOW,您的 sql 没有错误,这是因为您没有为连接放置任何谓词

select *, 
   row_number() over w 
from 
  units u
  left outer join leases l on u.id = l.unitid
  --                       ^^^^^^^^^^^^^^^^^^
window w AS (partition by u.id order by l.start desc)

If you're aiming to improve your sql knowledge you might want to skip use of the WINDOW clause because (as far as I'm aware) it's not supported by all DBMS;如果您的目标是提高您的 sql 知识,您可能希望跳过使用 WINDOW 子句,因为(据我所知)并非所有 DBMS 都支持它; learning syntax implemented by only some vendors when a more commonly implemented variant is available is unnecessarily limiting for you当有更普遍实现的变体可用时,学习仅由某些供应商实现的语法对您来说是不必要的限制

select *, 
   row_number() over (partition by u.id order by l.start desc) 
from
  units u
  left outer join leases l on u.id = l.unitid

What are you trying to do?你想做什么? You are not using any window functions.您没有使用任何窗口函数。

So the syntax is simply:所以语法很简单:

select *
from units u left outer join
     leases l 
     on <join conditions here>

The window clause would be used if you had a window function such as row_number() with an over clause.window子句将用于如果你有一个窗口的功能,如row_number()over子句。

You can add the window clause after the from clause:您可以在from子句之后添加window子句:

select *,
       row_number() over w 
from units u left outer join
     leases l 
     on <join conditions here>
window w as (partition by u.id order by l.start desc);

It is not needed unless you are going to refer to it more than once, so this is typically used:除非您要多次引用它,否则不需要它,因此通常使用它:

select *,
       row_number() over (partition by u.id order by l.start desc)
from units u left outer join
     leases l 
     on <join conditions here>;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM