From: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
---|---|
To: | Alexander Reichstadt <info(at)apfeltaste(dot)net> |
Cc: | pgsql-general(at)postgresql(dot)org |
Subject: | Re: DISTINCT ON changes sort order |
Date: | 2013-04-25 04:18:50 |
Message-ID: | 12846.1366863530@sss.pgh.pa.us |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
Alexander Reichstadt <info(at)apfeltaste(dot)net> writes:
> I think I solved it:
> SELECT * FROM (SELECT DISTINCT ON(refid_messages) refid_messages as msgid, * FROM messagehistorywithcontent WHERE (lower(ARRAY_TO_STRING(ARRAY[login,firstname,lastname,content,msgtitle], ' ')) LIKE '%gg%') ORDER BY refid_messages DESC) as foo ORDER BY messagekind ASC
This is not really a solution, unless you don't care which of the rows
with a particular refid_messages value comes out. Usually, use of
"DISTINCT ON (x)" should be accompanied by "ORDER BY x, y", where y is
what you want to determine which row of a given x value comes out.
If you write it like this, you get the row with smallest y for each
x; or you could write "ORDER BY x, y DESC" to get the row with largest
y. See the DISTINCT ON example in the SELECT reference page.
regards, tom lane
From | Date | Subject | |
---|---|---|---|
Next Message | Adarsh Sharma | 2013-04-25 05:46:37 | Re: Replication terminated due to PANIC |
Previous Message | Darren Duncan | 2013-04-25 04:17:14 | Re: custom session variables? |