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
, and value
. This table has a record of the names, the week at which they recorded the height, and the value of their height.
Something like this:
Name | Week | Value
------+--------+-------
John | 1 | 9
Cassie| 2 | 5
Luke | 6 | 3
John | 8 | 14
Cassie| 5 | 7
Luke | 9 | 5
John | 2 | 10
Cassie| 4 | 4
Luke | 7 | 4
What I want is a list per user of the value at the minimum week and the max week. Something like this:
Name |minWeek | Value |maxWeek | value
------+--------+-------+--------+-------
John | 1 | 9 | 8 | 14
Cassie| 2 | 5 | 5 | 7
Luke | 6 | 3 | 9 | 5
In Postgres, I use this query:
select name, week, value
from table t
inner join(
select name, min(week) as minweek
from table
group by name)
ss on t.name = ss.name and t.week = ss.minweek
group by t.name
;
However, I receive an error:
column "w.week" must appear in the GROUP BY clause or be used in an aggregate function
Position: 20
This worked fine for me in MySQL so I'm wondering what I'm doing wrong here?
There are various simpler and faster ways.
2x
DISTINCT ON
Or shorter:
Simple and easy to understand. Also fastest in my tests. Detailed explanation for
DISTINCT ON
:first_value()
of composite typeThe aggregate functions
min()
ormax()
do not accept composite types as input. You would have to create custom aggregate functions (which is not that hard).But the window functions
first_value()
andlast_value()
do. Building on that we can devise an very simple solutions:Simple query
The output has all data, but the values for the last week are stuffed into an anonymous record. You may need decomposed values.
Decomposed result with opportunistic use of table type
For that we need a well-known type that registers the types of contained elements with the system. An adapted table definition would allow for the opportunistic use of the table type itself directly:
week
andvalue
come first.Decomposed result from user-defined row type
However, that's probably not possible in most cases. Just use a user-defined type from
CREATE TYPE
(permanent) or fromCREATE TEMP TABLE
(for ad-hoc use):In a local test on Postgres 9.3 with a similar table of 50k rows, each of these queries was substantially faster than the currently accepted answer. Test with
EXPLAIN ANALYZE
.SQL Fiddle displaying all.
This is a bit of a pain, because Postgres has the nice window functions
first_value()
andlast_value()
, but these are not aggregation functions. So, here is one way: