I\'m new to Postgres, coming from MySQL and hoping that one of y\'all would be able to help me out.
I have a table with three columns: name
, week
This is a bit of a pain, because Postgres has the nice window functions first_value()
and last_value()
, but these are not aggregation functions. So, here is one way:
select t.name, min(t.week) as minWeek, max(firstvalue) as firstvalue,
max(t.week) as maxWeek, max(lastvalue) as lastValue
from (select t.*, first_value(value) over (partition by name order by week) as firstvalue,
last_value(value) over (partition by name order by week) as lastvalue
from table t
) t
group by t.name;