pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Moray McConnachie <moray.mcconnachie@computing-services.oxford.ac.uk>
To: PGSQL SQL <pgsql-sql@postgresql.org>
Subject: Maxima per row
Date: Fri, 7 Apr 2000 10:36:52 +0100
Message-ID: <00a201bfa074$ce92f050$760e01a3@oucs.ox.ac.uk> (raw)

I have a table, one of whose fields indicates the date from which
information in the row becomes valid, and another indicates the type to
which the row refers. How do I design a query such that I get one row for
each type, that row being the most recent info about that type? I.e. what I
want is

CREATE TABLE info (id serial primary key,typeid int4,validfrom
date,description text);
INSERT INTO info (typeid,validfrom,description) VALUES (1,'01/01/1999','a');
INSERT INTO info (typeid,validfrom,description) VALUES (1,'01/01/2000','b');
INSERT INTO info (typeid,validfrom,description) VALUES (2,'01/01/1998','c');
INSERT INTO info (typeid,validfrom,description) VALUES (2,'01/01/1999','d');
INSERT INTO info (typeid,validfrom,description) VALUES (2,'01/01/1997','e');

SELECT typeid,validfrom FROM prices GROUP BY typeid HAVING
validfrom=MAX(validfrom);

but of course the last is illegal. The result I'm looking for is

1,'01/01/2000','b',
2,'01/01/1999','d'

Thanks,
Moray

----------------------------------------------------------------
Moray.McConnachie@computing-services.oxford.ac.uk




view thread (2+ messages)  latest in thread

Message-ID: <00a201bfa074$ce92f050$760e01a3@oucs.ox.ac.uk>
Permalink:  ../00a201bfa074$ce92f050$760e01a3@oucs.ox.ac.uk/
Also on:    postgresql.org/message-id/00a201bfa074$ce92f050$760e01a3@oucs.ox.ac.uk

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: moray.mcconnachie@computing-services.oxford.ac.uk
  Subject: Re: Maxima per row
  In-Reply-To: <00a201bfa074$ce92f050$760e01a3@oucs.ox.ac.uk>
  References: <00a201bfa074$ce92f050$760e01a3@oucs.ox.ac.uk>

* 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