From: | Osvaldo Rosario Kussama <osvaldo(dot)kussama(at)gmail(dot)com> |
---|---|
To: | pgsql-general(at)postgresql(dot)org |
Subject: | Re: SELECT DISTINCT ON and ORDER BY |
Date: | 2008-03-28 20:08:32 |
Message-ID: | 47ED5040.6030903@gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
josep porres escreveu:
> maybe this?
>
> select value, max(id) as id, max(order_field) as order_field
> from mytable
> group by value
> order by 3
>
Wrong. For the op data you will obtain tuples not in original relation.
bdteste=# SELECT * FROM foo;
id | value | order_field
----+-------+-------------
1 | 10 | 3
2 | 12 | 4
3 | 10 | 1
4 | 5 | 8
5 | 12 | 2
(5 registros)
bdteste=# SELECT max(id), value, max(order_field)
FROM foo GROUP BY value
ORDER BY 3;
max | value | max
-----+-------+-----
3 | 10 | 3
5 | 12 | 4
4 | 5 | 8
(3 registros)
Try:
bdteste=# SELECT * FROM (
SELECT DISTINCT ON (value) id, value, order_field
FROM foo ORDER BY value, order_field) AS bar
ORDER BY order_field;
id | value | order_field
----+-------+-------------
3 | 10 | 1
5 | 12 | 2
4 | 5 | 8
(3 registros)
Osvaldo
From | Date | Subject | |
---|---|---|---|
Next Message | Sam Mason | 2008-03-28 20:16:03 | Re: dunction issue |
Previous Message | Tom Lane | 2008-03-28 19:41:47 | Re: pg_stat_user_tables |