agora inbox for pgsql-sql@postgresql.org  
help / color / mirror / Atom feed
removing duplicates and using sort
4+ messages / 3 participants
[nested] [flat]

* removing duplicates and using sort
@ 2013-09-15 02:36  Nathan Mailg <nathanmailg@gmail.com>
  0 siblings, 2 replies; 4+ messages in thread

From: Nathan Mailg @ 2013-09-15 02:36 UTC (permalink / raw)
  To: 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!

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: removing duplicates and using sort
@ 2013-09-16 14:04  Edward W. Rouse <erouse@comsquared.com>
  parent: Nathan Mailg <nathanmailg@gmail.com>
  1 sibling, 0 replies; 4+ messages in thread

From: Edward W. Rouse @ 2013-09-16 14:04 UTC (permalink / raw)
  To: 'Nathan Mailg' <nathanmailg@gmail.com>; pgsql-sql

Change the order by to order by lastname, firstname, refid, appldate

 

From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql-owner@postgresql.org]
On Behalf Of Nathan Mailg
Sent: Saturday, September 14, 2013 10:36 PM
To: pgsql-sql@postgresql.org
Subject: [SQL] removing duplicates and using sort

 

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!

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: removing duplicates and using sort
@ 2013-09-16 14:42  David Johnston <polobo@yahoo.com>
  parent: Nathan Mailg <nathanmailg@gmail.com>
  1 sibling, 1 reply; 4+ messages in thread

From: David Johnston @ 2013-09-16 14:42 UTC (permalink / raw)
  To: pgsql-sql

Note that you could always do something like:

WITH original_query AS (
SELECT DISTINCT ...
)
SELECT *
FROM original_query
ORDER BY lastname, firstname;

OR

SELECT * FROM (
    SELECT DISTINCT ....
) sub_query
ORDER BY lastname, firstname

I am thinking you cannot alter the existing ORDER BY otherwise your use of
"DISTINCT ON" begins to mal-function.  I dislike DISTINCT ON generally but
do not wish to ponder how you can avoid it, so I'd suggest just turning your
query into a sub-query like I show above.

David J.




--
View this message in context: http://postgresql.1045698.n5.nabble.com/removing-duplicates-and-using-sort-tp5770931p5771096.html
Sent from the PostgreSQL - sql mailing list archive at Nabble.com.


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



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: removing duplicates and using sort
@ 2013-09-17 16:03  Nathan Mailg <nathanmailg@gmail.com>
  parent: David Johnston <polobo@yahoo.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Nathan Mailg @ 2013-09-17 16:03 UTC (permalink / raw)
  To: pgsql-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



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2013-09-17 16:03 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2013-09-15 02:36 removing duplicates and using sort Nathan Mailg <nathanmailg@gmail.com>
2013-09-16 14:04 ` Edward W. Rouse <erouse@comsquared.com>
2013-09-16 14:42 ` David Johnston <polobo@yahoo.com>
2013-09-17 16:03   ` Nathan Mailg <nathanmailg@gmail.com>

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