agora inbox for pgsql-sql@postgresql.org  
help / color / mirror / Atom feed
From: Nathan Mailg <nathanmailg@gmail.com>
To: pgsql-sql@postgresql.org
Subject: Re: removing duplicates and using sort
Date: Tue, 17 Sep 2013 12:03:15 -0400
Message-ID: <309ABE98-2643-4E6B-B5C9-FF68271B9661@gmail.com> (raw)
In-Reply-To: <1379342569452-5771096.post@n5.nabble.com>
References: <CAN8Rhd2n84Yxd4QP0SXuyuO+K4f3scQnAYAwq53GDFojOSkAjw@mail.gmail.com>
	<1379342569452-5771096.post@n5.nabble.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-sql>

Yes, that's correct, modifying the original ORDER BY gives:

ORDER BY lastname, firstname, refid, appldate DESC;
ERROR:  SELECT DISTINCT ON expressions must match initial ORDER BY expressions

Using WITH works great:

WITH distinct_query AS (
    SELECT DISTINCT ON (refid) id, refid, lastname, firstname, appldate
        FROM appl WHERE lastname ILIKE 'Williamson%' AND firstname ILIKE 'd%'
        GROUP BY refid, id, lastname, firstname, appldate
        ORDER BY refid, appldate DESC
    )
SELECT * FROM distinct_query ORDER BY lastname, firstname;

Thank you!



-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql



view thread (4+ messages)

Message-ID: <309ABE98-2643-4E6B-B5C9-FF68271B9661@gmail.com>
Permalink:  ../309ABE98-2643-4E6B-B5C9-FF68271B9661@gmail.com/
Also on:    postgresql.org/message-id/309ABE98-2643-4E6B-B5C9-FF68271B9661@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: nathanmailg@gmail.com
  Subject: Re: removing duplicates and using sort
  In-Reply-To: <309ABE98-2643-4E6B-B5C9-FF68271B9661@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