

The following example uses he PostgreSQL rank() function. Numeric, double, otherwise same datatype as the argumentīigint, otherwise same datatype as the argument Numeric, string, date/time, network or enum type Window functions in PostgreSQL operate on a logical partition or window of the result set and return a value for rows in that window.įrom a database migration perspective, you should examine PostgreSQL window functions by type and compare them with the equivalent SQL Server window functions to verify compatibility of syntax and output.

They provide the same core functionality as SQL Server analytical functions. PostgreSQL refers to ANSI SQL analytical functions as window functions. Item Quantity OrderID NextQtyOrderįor more information, see SELECT - OVER Clause (Transact-SQL) in the SQL Server documentation. The preceding example produces the following results. OVER (PARTITION BY OrderID ORDER BY Quantity) AS NextQtyOrder The following example uses an analytic LEAD function to get the next largest quantity for the order. OVER (PARTITION BY OrderID) AS TotalOrderQty This statement doesn’t use a GROUP BY clause. The following example uses a partitioned window aggregate function to calculate the total quantity for each order. RANK() OVER(ORDER BY Quantity) AS QtyRank The following example uses a window ranking function to rank items based on the ordered quantity. ) INSERT INTO OrderItems (OrderID, Item, Quantity)

The following example creates and populates an OrderItems table. For more information, see Sequences and Identity. Ranking functions: ROW_NUMBER, RANK, DENSE_RANK, and NTILE.Īggregate functions: AVG, MIN, MAX, SUM, COUNT, COUNT_BIG, VAR, STDEV, STDEVP, STRING_AGG, GROUPING, GROUPING_ID, VAR, VARP, and CHECKSUM_AGG.Īnalytic functions: LAG, LEAD, FIRST_Value, LAST_VALUE, PERCENT_RANK, PERCENTILE_CONT, PERCENTILE_DISC, and CUME_DIST.
