FAQ
I was under impression that select distinct on (xx) ...
will fail if xx doesn't match the left most part of order by. i.e. it
requires order by xx, while allowing order by xx, something, else.

But it seems you can run the query with no order by clause at all.

is it intentional?

# select distinct on (x) x, y from test;
x | y
---+---
1 | 2
2 | 3
(2 rows)

# select * from test;
x | y
---+---
1 | 2
2 | 3
1 | 4
(3 rows)

Best regards,

depesz

--
Linkedin: http://www.linkedin.com/in/depesz / blog: http://www.depesz.com/
jid/gtalk: depesz@depesz.com / aim:depeszhdl / skype:depesz_hdl / gg:6749007

Search Discussions

  • Jeff Davis at May 18, 2009 at 10:55 pm

    On Mon, 2009-05-18 at 20:24 +0200, hubert depesz lubaczewski wrote:
    I was under impression that select distinct on (xx) ...
    will fail if xx doesn't match the left most part of order by. i.e. it
    requires order by xx, while allowing order by xx, something, else.

    But it seems you can run the query with no order by clause at all.

    is it intentional?
    This is documented behavior:

    "Note that the 'first row' of each set is unpredictable unless ORDER BY
    is used to ensure that the desired row appears first."

    http://www.postgresql.org/docs/8.4/static/sql-select.html#SQL-DISTINCT

    Regards,
    Jeff Davis
  • Tom Lane at May 18, 2009 at 10:57 pm

    hubert depesz lubaczewski writes:
    I was under impression that select distinct on (xx) ...
    will fail if xx doesn't match the left most part of order by. i.e. it
    requires order by xx, while allowing order by xx, something, else.
    No, it requires that *if* you specify an ORDER BY, it agrees with
    the DISTINCT ON.

    regards, tom lane

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppgsql-bugs @
categoriespostgresql
postedMay 18, '09 at 6:24p
activeMay 18, '09 at 10:57p
posts3
users3
websitepostgresql.org
irc#postgresql

People

Translate

site design / logo © 2022 Grokbase