它的定义是这样的:”A window function performs a calculation across a set of table rows that are somehow related to the current row. “。
说那么多没什么用,直接看例子。
-----------+-------+--------+-----------------------
develop | 11 | 5200 | 5020.0000000000000000
develop | 7 | 4200 | 5020.0000000000000000
develop | 9 | 4500 | 5020.0000000000000000
develop | 10 | 5200 | 5020.0000000000000000
personnel | 5 | 3500 | 3700.0000000000000000
personnel | 2 | 3900 | 3700.0000000000000000
sales | 3 | 4800 | 4866.6666666666666667
sales | 4 | 4800 | 4866.6666666666666667
(10 rows)
在某些场合下,这种肯定没有“Window Functions”,因为salary和empno只有一个了。假如我们需要输出salary和empno的话,只能再查一次,然后用程序循环出来,只能这样组合了。在实际的开发中,是遇到过这种问题的。而PostgreSQL默认就提供了“Window Function”机制来解决这一问题,很方便。
还支持排序。
SELECT depname, empno, salary,
rank() OVER (PARTITION BY depname ORDER BY salary DESC)
Article.find_by_sql("SELECT *, rank() OVER (ORDER BY group_id DESC) FROM articles")
结合一些图表统计的库,比如hightcharts,可以实现类似这样的效果。