Re: only last records in subgroups

From: Achilleus Mantzios <achill(at)matrix(dot)gatewaynet(dot)com>
To: Dino Vliet <dino_vliet(at)yahoo(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: only last records in subgroups
Date: 2004-08-17 11:35:04
Message-ID: Pine.LNX.4.44.0408171434210.31368-100000@matrix.gatewaynet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

O kyrios Dino Vliet egrapse stis Aug 17, 2004 :

> Hi there,
>
> I'm having hard times with the following query:
> I want to select only the last records from a subgroup
> in a table. But because the subgroup contains
> different number of records for every id, I don't know
> how to specify that. For example, check the following
> table,
> id, day
> x,300
> x,250,
> x,0
> y,250
> y,4
>
> I only want the records
> x,0 and y,4 but how do I manage this in sql? I was
> hoping for a keyword LAST or so, where I can specify
> that when I've ordered my results with order by, I
> could only get the last of the subgroups (the first
> one is easy because I could use limit 1)
>

Try,

SELECT min(day),id from reg group by id;
min | id
-----+----
4 | y
0 | x
(2 rows)

> Thanks in advance
>
>
>
>
> __________________________________
> Do you Yahoo!?
> New and Improved Yahoo! Mail - 100MB free storage!
> http://promotions.yahoo.com/new_mail
>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
>
> http://www.postgresql.org/docs/faqs/FAQ.html
>

--
-Achilleus

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Kaloyan Iliev Iliev 2004-08-17 13:16:31 Re: Inheriting text[] field
Previous Message Markus Bertheau 2004-08-17 11:07:43 SELECT MAX(c) FROM (SELECT ... FOR UPDATE) AS foo