agora inbox for pgsql-sql@postgresql.org  
help / color / mirror / Atom feed
From: Mark Williams <markwillimas@gmail.com>
To: 'David G. Johnston' <david.g.johnston@gmail.com>
Cc: 'pgsql-sql' <pgsql-sql@lists.postgresql.org>
Subject: RE: Select Distinct Order By Array_Position
Date: Mon, 26 Nov 2018 19:59:55 -0000
Message-ID: <005301d485c2$9a6f1db0$cf4d5910$@gmail.com> (raw)
In-Reply-To: <CAKFQuwYroR1Q_+rF6odeDEA5+NLk6TjCYy-yTJUxf+ob7huhdg@mail.gmail.com>
References: <004301d485bc$00347250$009d56f0$@gmail.com>
	<CAKFQuwYroR1Q_+rF6odeDEA5+NLk6TjCYy-yTJUxf+ob7huhdg@mail.gmail.com>

David, thanks. It was a bad example on my part. Could just as well have been: (26194, 189, 2345).

__

-----Original Message-----
From: David G. Johnston <david.g.johnston@gmail.com> 
Sent: 26 November 2018 19:46
To: Mark Williams <markwillimas@gmail.com>
Cc: pgsql-sql <pgsql-sql@lists.postgresql.org>
Subject: Re: Select Distinct Order By Array_Position

On Mon, Nov 26, 2018 at 12:12 PM Mark Williams <markwillimas@gmail.com> wrote:
> I am ordering by documents.id and it appears in my select list.
[...]
> SELECT DISTINCT documents.id, page_no FROM texts LEFT JOIN documents 
> on documents.id=texts.doc_id WHERE doc_id IN (26194, 2345, 189) AND  
> (text LIKE '%RIVER%') ORDER BY array_position(ARRAY[26194, 2345, 
> 189]::INTEGER[], documents.id)

No, you are not ordering by documents.id, you are ordering by an expression into which you are passing the documents.id value as one of its components.

When you use ORDER BY and DISTINCT together you basically are short-handing:

SELECT sq.*
FROM (SELECT DISTINCT ...) AS sq
ORDER BY sq.?

If you want to order by something you have to include it exactly in the select-list of the inner/distinct query.

In this example, though, you could just "ORDER BY documents.id DESC"...

David J.





view thread (7+ messages)  latest in thread

Message-ID: <005301d485c2$9a6f1db0$cf4d5910$@gmail.com>
Permalink:  ../005301d485c2$9a6f1db0$cf4d5910$@gmail.com/
Also on:    postgresql.org/message-id/005301d485c2$9a6f1db0$cf4d5910$@gmail.com

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: markwillimas@gmail.com, david.g.johnston@gmail.com, pgsql-sql@lists.postgresql.org
  Subject: RE: Select Distinct Order By Array_Position
  In-Reply-To: <005301d485c2$9a6f1db0$cf4d5910$@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox