SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

SQL Server's implementation of Window Functions

By Phil Factor,

PostgreSQL v9 is out. It is greatly improved. Why should you be interested? If you were a petrol-head, you’d leap into a different car for that sunny weekend, feeling refreshed, even after a dreary week stuck in traffic in your commuting car. Well, I’m a SQL-Head, and it is a great pleasure to open the metaphorical garage doors, admire the metaphorical chrome, and take the metaphorical sporty convertible around the country roads.  PostgreSQL was once the Lada, or Trabant of databases, but that’s all changed. It is fast, 64 bit, easy to install, and starts on first prod.

After a quick spin, it’s the Window functions that impress me the most. Whereas SQL Server’s adoption of the Window functions (think TOP,  ROW_NUMBER, RANK, DENSE_RANK and NTILE) has been tentative, PostgreSQL, like many competitor RDBMSs,  has a good implementation. You’re aware of what you’re missing when you try things out in PostgreSQL..  Window functions allow you to define the ordering for calculations without violating any relational principles, and, by doing so, eliminate the last remaining requirement for iterative or cursor-based calculations. They provide the means to perform calculations across sets of rows. SQL Server 2005 took the unfortunate course of implementing some, but not all, of the window functions. The essential ORDER BY clause of Window Aggregate functions, for example, was omitted, so you can’t do running totals. The wonderful LAG and LEAD offset functions are missing, so you have to do trend analysis a really clunky way.  Functions that support statistical analysis aren’t implemented. You cannot name and reuse your windows, or have multiple windows in a query.

PostgreSQL, by contrast, manages to exploit the full use of Window Functions in its optimiser to produce fast queries. Everything seems to work fine.  After trying out several examples, it was a wrench to go back to SQL Server 2008 to do the sort of operations that are so simple to perform with full support for window functions.

 It was like going back into the company car on Monday after a weekend cruising in a  sporty rag-top

Total article views: 779 | Views in the last 30 days: 3
Related Articles


PostgreSQL PostgreSQL has always been interesting to me and has captivated me more than MySQL...


Azure support MySQL & PostgreSQL

After SQL Server support on Linux Microsoft is not have another great enhancement where now Azure su...


Windowing Functions: Tell me when that changes

Windowing functions aren't just for analyzing numbers/dates. Using windows functions to analyze when...


lag function calculation

llag function calculation


Solving FIFO Queues Using Windowed Functions

One common request often seen in T-SQL is working with queues of information. In this piece, J. Drew...

database weekly