removing duplicates and using sort

From: Nathan Mailg <nathanmailg(at)gmail(dot)com>
To: "pgsql-sql(at)postgresql(dot)org" <pgsql-sql(at)postgresql(dot)org>
Subject: removing duplicates and using sort
Date: 2013-09-15 02:36:21
Message-ID: CAN8Rhd2n84Yxd4QP0SXuyuO+K4f3scQnAYAwq53GDFojOSkAjw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

I'm using 8.4.17 and I have the following query working, but it's not quite
what I need:

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;

I worked on this awhile and is as close as I could get. So this returns
rows as you'd expect, except I need to somehow modify this query so it
returns the rows ordered by lastname, then firstname.

I'm using distinct so I get rid of duplicates in the table where refid (an
integer) is used as the common id that ties like records together. In other
words, I'm using it to get only the most recent appldate (a date) for each
group of refid's that match the lastname, firstname where clause.

I just need the rows returned from the query above to be sorted by
lastname, then firstname.

Hope I explained this well enough. Please let me know if you need more info.

Thanks!

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Edward W. Rouse 2013-09-16 14:04:40 Re: removing duplicates and using sort
Previous Message rawi 2013-09-11 12:00:02 Re: apt.postgresql.org vs. Pitti PPA - install error