简体   繁体   English

如何通过窗口函数为聚合函数过滤SQL语句中的行?

[英]How to filter rows in SQL statement for aggregate function by window function?

I have some table and provide tools to the user to generate new columns based on existings. 我有一些表,并为用户提供了根据现有数据生成新列的工具。

Table: 表:

+---+
|  a|
+---+
|  0|
|  1|
|  2|
|  3|
|  4|
|  5|
+---+

New column name: b 新列名称: b

New column rule must be like: max(a) over(WHERE a < 3) 新列规则必须类似于: max(a) over(WHERE a < 3)

How to correct write this? 如何正确写呢?

Result must be like SQL statement: SELECT *, (SELECT max(a) FROM table WHERE a < 3) as b FROM table . 结果必须类似于SQL语句: SELECT *, (SELECT max(a) FROM table WHERE a < 3) as b FROM table And returns: 并返回:

+---+---+
|  a|  b|
+---+---+
|  0|  2|
|  1|  2|
|  2|  2|
|  3|  2|
|  4|  2|
|  5|  2|
+---+---+

But I can't wrote inside over() WHERE statement and can't allow user to know name of table. 但是我无法在over()WHERE语句中编写内容,也无法允许用户知道表的名称。 How do I solve this problem? 我该如何解决这个问题?

Just use a window function with case : 只需使用带有case的window函数:

select a, max(case when a < 3 then a end) over () as b
from t;

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

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