您的位置:首页 > 新闻 > 资讯 > 安卓手机软件开发平台_青岛网站制作费用_搜狗搜索引擎优化论文_如何制作网页广告

安卓手机软件开发平台_青岛网站制作费用_搜狗搜索引擎优化论文_如何制作网页广告

2025/4/18 23:44:07 来源:https://blog.csdn.net/weixin_45594127/article/details/146145696  浏览:    关键词:安卓手机软件开发平台_青岛网站制作费用_搜狗搜索引擎优化论文_如何制作网页广告
安卓手机软件开发平台_青岛网站制作费用_搜狗搜索引擎优化论文_如何制作网页广告

正常使用:相当于对每个row做一次子查询

SELECT depname, empno, salary, avg(salary) OVER (PARTITION BY depname) FROM empsalary;

order by 区别window frame and partition

没有order by, window function是对整个partition起作用, partition是over语句制定的
有了order by,那window function就是对window frame起作用,window frame就是整个partition先按照order by排序,然后到当前row为止的所有数据。
There is another important concept associated with window functions: for each row, there is a set of rows within its partition called its window frame. Some window functions act only on the rows of the window frame, rather than of the whole partition. By default, if ORDER BY is supplied then the frame consists of all rows from the start of the partition up through the current row, plus any following rows that are equal to the current row according to the ORDER BY clause. When ORDER BY is omitted the default frame consists of all rows in the partition. [5] Here is an example using sum:

SELECT salary, sum(salary) OVER () FROM empsalary;salary |  sum  
--------+-------5200 | 471005000 | 471003500 | 471004800 | 471003900 | 471004200 | 471004500 | 471004800 | 471006000 | 471005200 | 47100
(10 rows)

没有order by, sum的结果是一样的,因为只有一个partition。

SELECT salary, sum(salary) OVER (ORDER BY salary) FROM empsalary;salary |  sum  
--------+-------3500 |  35003900 |  74004200 | 116004500 | 161004800 | 257004800 | 257005000 | 307005200 | 411005200 | 411006000 | 47100
(10 rows)

有了order by,每次只计算不断增长的window frame

可复用的window表达式

SELECT sum(salary) OVER w, avg(salary) OVER wFROM empsalaryWINDOW w AS (PARTITION BY depname ORDER BY salary DESC);

window function执行顺序

window function是在where, group执行完以后才执行的,所以不可以用在where, group语句里面,只能用在select,order by子语句。
Window functions are permitted only in the SELECT list and the ORDER BY clause of the query. They are forbidden elsewhere, such as in GROUP BY, HAVING and WHERE clauses. This is because they logically execute after the processing of those clauses. Also, window functions execute after non-window aggregate functions. This means it is valid to include an aggregate function call in the arguments of a window function, but not vice versa.

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com