Help Retrieving Latest Record

Поиск
Список
Период
Сортировка
Искать

Help Retrieving Latest Record

От:
Steve Meynell <steve@candata.com>
Дата:
 Hi,  I am not sure if this went through the first time so here it is again. 
 

Ok what I am trying to do is select out of the database the latest record meeting a certain criteria.

Example:

Number             |        Fruit         |     Date
15                                Apples          July 20, 1999
20                                Oranges        June 7, 2000
13                                 Pears            Jan 31, 2000
17                                 Apples         April 10, 1999
Now what I need to do is select the oranges out because the date is the latest one, something like:

select * from basket where max(date);
This would yield me:
20                                Oranges        June 7, 2000

I know this doesn't work but I need something like it.
or something like

select * from basket where max(date) and fruit='Apples';
This would yield me:
15                                Apples          July 20, 1999

Thank you in advance,
 

-- 
Steve Meynell
Candata Systems
 

Re: Help Retrieving Latest Record

От:
Dorin Grunberg <dorin@visgen.com>
Дата:
It would be something like:

select * from basket where Date=(SELECT max(Date) from basket);



At 09:41 AM 2/16/2001 -0500, Steve Meynell wrote:
>Hi,  I am not sure if this went through the first time so here it is again.
>
>
>Ok what I am trying to do is select out of the database the latest record 
>meeting a certain criteria.
>
>Example:
>
>Number             |        Fruit         |     Date
>15                                Apples          July 20, 1999
>20                                Oranges        June 7, 2000
>13                                 Pears            Jan 31, 2000
>17                                 Apples         April 10, 1999
>Now what I need to do is select the oranges out because the date is the 
>latest one, something like:
>
>select * from basket where max(date);
>This would yield me:
>20                                Oranges        June 7, 2000
>
>I know this doesn't work but I need something like it.
>or something like
>
>select * from basket where max(date) and fruit='Apples';
>This would yield me:
>15                                Apples          July 20, 1999
>
>Thank you in advance,
>
>--
>Steve Meynell
>Candata Systems


Re: Help Retrieving Latest Record

От:
Roberto Mello <rmello@cc.usu.edu>
Дата:
On Fri, Feb 16, 2001 at 09:41:09AM -0500, Steve Meynell wrote:
> 
> I know this doesn't work but I need something like it.
> or something like
> 
> select * from basket where max(date) and fruit='Apples';
> This would yield me:
> 15                                Apples          July 20, 1999
Maybe you mean min? This is the earliest date you had in the set.
Assuming the Date column is of datetime/timestamp type:

test=# \d fruits               Table "fruits"Attribute |         Type          | Modifier
-----------+-----------------------+----------fruit_id  | integer               |name      | character varying(50) |date      | timestamp             |

test=# select * from fruits;fruit_id |   name    |          date
----------+-----------+------------------------       1 | Orange    | 2001-02-16 13:26:52-07       3 | Pineapple | 1999-10-04 00:00:00-06       2 | Apple     | 2000-12-05 00:00:00-07       4 | Apple     | 2000-07-01 00:00:00-06
(4 rows)

test=#  SELECT * FROM fruits WHERE name='Apple' ORDER BY date DESC LIMIT
1;fruit_id | name  |          date
----------+-------+------------------------       2 | Apple | 2000-12-05 00:00:00-07
(1 row)
Look at the documentation for the ORDER BY and LIMIT clauses on the PG
docs.
-Roberto
-- 
Computer Science			Utah State University
Space Dynamics Laboratory		Web Developer
USU Free Software & GNU/Linux Club 	http://fslc.usu.edu
My home page - http://www.brasileiro.net/roberto

Re: Help Retrieving Latest Record

От:
Michael Fork <mfork@toledolink.com>
Дата:
SELECT * FROM basket ORDER BY date desc LIMIT 1;
and
SELECT * FROM basket WHERE fruit = 'apple' ORDER BY date desc LIMIT 1;

Michael Fork - CCNA - MCP - A+
Network Support - Toledo Internet Access - Toledo Ohio

On Fri, 16 Feb 2001, Steve Meynell wrote:

> Hi,  I am not sure if this went through the first time so here it is
> again.
> 
> 
> Ok what I am trying to do is select out of the database the latest
> record meeting a certain criteria.
> 
> Example:
> 
> Number             |        Fruit         |     Date
> 15                                Apples          July 20, 1999
> 20                                Oranges        June 7, 2000
> 13                                 Pears            Jan 31, 2000
> 17                                 Apples         April 10, 1999
> Now what I need to do is select the oranges out because the date is the
> latest one, something like:
> 
> select * from basket where max(date);
> This would yield me:
> 20                                Oranges        June 7, 2000
> 
> I know this doesn't work but I need something like it.
> or something like
> 
> select * from basket where max(date) and fruit='Apples';
> This would yield me:
> 15                                Apples          July 20, 1999
> 
> Thank you in advance,
> 
> 
> --
> Steve Meynell
> Candata Systems
> 
> 
> 


Re: Help Retrieving Latest Record

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Steve Meynell  writes:
> select * from basket where max(date) and fruit='Apples';
> I know this doesn't work but I need something like it.

Perhaps
select * from basket where fruit='Apples'	order by date desc	limit 1;

The limit clause is not standard SQL, but it's a pretty easy answer
if portability doesn't bother you...
		regards, tom lane

Re: Help Retrieving Latest Record

От:
Jie Liang <jliang@ipinc.com>
Дата:

Subquery will do:

select * from basket where Date in (select max(Date) from basket);

Jie LIANG

St. Bernard Software
Internet Products Inc.

10350 Science Center Drive
Suite 100, San Diego, CA 92121
Office:(858)320-4873

jliang@ipinc.com
www.stbernard.com
www.ipinc.com

On Fri, 16 Feb 2001, Steve Meynell wrote:

> Hi,  I am not sure if this went through the first time so here it is
> again.
> 
> 
> Ok what I am trying to do is select out of the database the latest
> record meeting a certain criteria.
> 
> Example:
> 
> Number             |        Fruit         |     Date
> 15                                Apples          July 20, 1999
> 20                                Oranges        June 7, 2000
> 13                                 Pears            Jan 31, 2000
> 17                                 Apples         April 10, 1999
> Now what I need to do is select the oranges out because the date is the
> latest one, something like:
> 
> select * from basket where max(date);
> This would yield me:
> 20                                Oranges        June 7, 2000
> 
> I know this doesn't work but I need something like it.
> or something like
> 
> select * from basket where max(date) and fruit='Apples';
> This would yield me:
> 15                                Apples          July 20, 1999
> 
> Thank you in advance,
> 
> 
> --
> Steve Meynell
> Candata Systems
> 
> 
> 


FAQ