pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Lane Van Ingen <lvaningen@esncc.com>
To: pgsql-sql@postgresql.org
Subject: Can EXCEPT Be Used for To Solve This Problem?
Date: Wed, 24 Aug 2005 16:47:16 -0400
Message-ID: <EKEMKEFLOMKDDLIALABIMEMGCBAA.lvaningen@esncc.com> (raw)

Given the following data in a table named 'foo' :
     id   update_time           description
     2    2005-08-24 00:10:00   transaction1
     2    2005-08-24 00:22:00   transaction2
     2    2005-08-24 00:34:00   transaction3
     2    2005-08-24 00:58:00   transaction4

I want to select 2nd oldest transaction from foo (transaction 3). The
solution below
works, but I think there may be a better way. Does anyone else have a better
idea?

select * from foo f1
  join
  (select id, update_time
   from foo
       except
   select id, max(update_time) as update_time
   from foo
   group by id) f2
using (id, update_time)
order by 1, 2 desc limit 1;





view thread (6+ messages)  latest in thread

Message-ID: <EKEMKEFLOMKDDLIALABIMEMGCBAA.lvaningen@esncc.com>
Permalink:  ../EKEMKEFLOMKDDLIALABIMEMGCBAA.lvaningen@esncc.com/
Also on:    postgresql.org/message-id/EKEMKEFLOMKDDLIALABIMEMGCBAA.lvaningen@esncc.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: lvaningen@esncc.com
  Subject: Re: Can EXCEPT Be Used for To Solve This Problem?
  In-Reply-To: <EKEMKEFLOMKDDLIALABIMEMGCBAA.lvaningen@esncc.com>

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

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