pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Julie Robinson <funkjunk@bellsouth.net>
To: pgsql-sql@postgresql.org
Subject: Re: SQL Query Newbie Help
Date: Fri, 24 Mar 2006 11:50:57 -0600
Message-ID: <1228cchdf3qqfe@corp.supernews.com> (raw)
In-Reply-To: <12287b48nref60f@corp.supernews.com>
References: <12287b48nref60f@corp.supernews.com>

This works, but is there a better solution?

select *
from quality_control_reset T
where date = (
    select max(date)
    from quality_control_reset
    where qualitycontrolrange = T.qualitycontrolrange);


Julie Robinson wrote:
> Given the two tables at the bottom of this email, I'm having trouble 
> coming up with a SQL statement that returns all rows in the 
> quality_control_reset table where there is only one row for the most 
> recent quality_control_range.  Help?
> 
> Example:
> 
> In table quality_control_reset:
> 
> id   |   date        |   qualitycontrolrange
> ---------------------------------------------
> 1    |  02/23/2006   |   20
> 2    |  02/23/2006   |   6
> 3    |  02/28/2006   |   18
> 4    |  03/01/2006   |   18
> 5    |  03/23/2006   |   12
> 6    |  03/23/2006   |   20
> 
> I want the results of the following from the query:
> 
> id   |   date        |   qualitycontrolrange
> ---------------------------------------------
> 2    |  02/23/2006   |   6
> 4    |  03/01/2006   |   18
> 5    |  03/23/2006   |   12
> 6    |  03/23/2006   |   20
> 
> 
> CREATE TABLE quality_control_reset
> (
>   id int8 NOT NULL,
>   date timestamp,
>   qualitycontrolrange int8,
>   CONSTRAINT quality_control_reset_pkey PRIMARY KEY (id),
>   CONSTRAINT fk42a706efb62efa94 FOREIGN KEY (qualitycontrolrange)
>       REFERENCES quality_control_range (id) MATCH SIMPLE
>       ON UPDATE NO ACTION ON DELETE NO ACTION
> )
> 
> CREATE TABLE quality_control_range (
>   id int8 NOT NULL,
>   code varchar(255),
>   CONSTRAINT quality_control_range_pkey PRIMARY KEY (id)
> );



view thread (4+ messages)  latest in thread

Message-ID: <1228cchdf3qqfe@corp.supernews.com>
Permalink:  ../1228cchdf3qqfe@corp.supernews.com/
Also on:    postgresql.org/message-id/1228cchdf3qqfe@corp.supernews.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: funkjunk@bellsouth.net
  Subject: Re: SQL Query Newbie Help
  In-Reply-To: <1228cchdf3qqfe@corp.supernews.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