Search code examples
sqlpostgresqlwindow-functions

Applying Multiple Window Functions On Same Partition


Is it possible to apply multiple window functions to the same partition? (Correct me if I'm not using the right vocabulary)

For example you can do

SELECT name, first_value() over (partition by name order by date) from table1

But is there a way to do something like:

SELECT name, (first_value() as f, last_value() as l (partition by name order by date)) from table1

Where we are applying two functions onto the same window?

Reference: http://postgresql.ro/docs/8.4/static/tutorial-window.html


Solution

  • Can you not just use the window per selection

    Something like

    SELECT  name, 
            first_value() OVER (partition by name order by date) as f, 
            last_value() OVER (partition by name order by date) as l 
    from table1
    

    Also from your reference you can do it like this

    SELECT sum(salary) OVER w, avg(salary) OVER w
    FROM empsalary
    WINDOW w AS (PARTITION BY depname ORDER BY salary DESC)