Re: SQL subqueries newbie help

From: Alexei Chetroi <lexoid(at)lexa(dot)uniflux-line(dot)net>
To: Rod Taylor <rbt(at)rbt(dot)ca>
Cc: Alexei Chetroi <lexoid(at)lexa(dot)uniflux-line(dot)net>, Pgsql-Sql <pgsql-sql(at)postgresql(dot)org>
Subject: Re: SQL subqueries newbie help
Date: 2003-09-07 11:42:31
Message-ID: 20030907114226.GA22231@uniflux-line.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Sat, Sep 06, 2003 at 01:21:36PM -0400, Rod Taylor wrote:
> > I'd like to write a query which returns following information regarding
> > each item: item, date of very first event, very last event.
> > Is this possible? I think I can write several SELECT queries and
> > procces them by an application or possibly write some procedure, but
> > what is better solution?
>
> Something like the below should do it:
>
> SELECT item
> , min_date
> , min_event
> , max_date
> , max_event
> FROM items
> JOIN (SELECT min(date) AS min_date
> , event AS min_event
> , item
> FROM events
> GROUP BY item) AS mn USING (item)
[skip]

Thanks everybody for responses. I'm trying this one, but psql complains
on queries like "SELECT min(date), event FROM events GROUP BY item" that
events must be GROUPed or used in an aggregate function. Why this
happens and why it needs be so?

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Rod Taylor 2003-09-07 13:09:35 Re: SQL subqueries newbie help
Previous Message Mark Stosberg 2003-09-07 00:38:43 Re: recursive sql (using the sort_key method)