Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create named window partitions (aliases) in PostgreSQL?

The documentation for PostgreSQL window functions seems to imply you can use the same named window in multiple places in your query. However, I can't figure out how do I create a named window?

SELECT first_value(vin) OVER( PARTITION BY vin ) AS w, first_value(make) OVER w
FROM inventory.vehicles
WHERE lot_Id = 9999 AND make is not null;

This is a joke query I'm trying to get the syntax to take, but I'm getting error:

ERROR: window "w" does not exist

like image 447
NO WAR WITH RUSSIA Avatar asked Aug 10 '10 23:08

NO WAR WITH RUSSIA


1 Answers

The answer was actually in the SELECT doc:

WINDOW Clause

The optional WINDOW clause has the general form

WINDOW window_name AS ( window_definition ) [, ...]

Here is an example,

SELECT first_value(vin) OVER w,
  first_value(make) OVER w
FROM inventory.vehicles
WHERE lot_Id = 9999
  AND make is not null
WINDOW w AS ( PARTITION by vin );
like image 108
NO WAR WITH RUSSIA Avatar answered Oct 13 '22 01:10

NO WAR WITH RUSSIA