Re: newbie question for return date

Lists: pgsql-general
From: "tviardot" <tviardot(at)sympatico(dot)ca>
To: pgsql-general(at)postgresql(dot)org
Subject: newbie question for return date
Date: 2002-10-28 19:57:19
Message-ID: xEgv9.2668$h_4.374526@news20.bellglobal.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Hi guys, here a newbies question.
I've made a table with some action and date.
How may i query the most recent date. (I'd like to return only the record
which have the most recent date ).
Tx


From: Lee Harr <missive(at)frontiernet(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: newbie question for return date
Date: 2002-10-29 00:52:19
Message-ID: apkm43$8j$1@news.hub.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

In article <xEgv9(dot)2668$h_4(dot)374526(at)news20(dot)bellglobal(dot)com>, tviardot wrote:
> Hi guys, here a newbies question.
> I've made a table with some action and date.
> How may i query the most recent date. (I'd like to return only the record
> which have the most recent date ).
> Tx
>
>

How about:

SELECT * FROM t ORDER BY d DESC LIMIT 1;


From: Medi Montaseri <medi(dot)montaseri(at)intransa(dot)com>
To: missive(at)hotmail(dot)com
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: newbie question for return date
Date: 2002-10-29 01:12:42
Message-ID: 3DBDE08A.9030009@intransa.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Hey a suggestion, what if PG would support the negative limit as in
select * from table limit -1 to mean limit it from the other end of
list.....

Sort of like some languages where they support

array[1] vs array[-1].

I'm not sure, but it looks like order by will sort the list which is
expensive and then
allow us to get the first chunk specified by limit.

Lee Harr wrote:

>In article <xEgv9(dot)2668$h_4(dot)374526(at)news20(dot)bellglobal(dot)com>, tviardot wrote:
>
>
>>Hi guys, here a newbies question.
>>I've made a table with some action and date.
>>How may i query the most recent date. (I'd like to return only the record
>>which have the most recent date ).
>>Tx
>>
>>
>>
>>
>
>How about:
>
>SELECT * FROM t ORDER BY d DESC LIMIT 1;
>
>
>---------------------------(end of broadcast)---------------------------
>TIP 3: if posting/reading through Usenet, please send an appropriate
>subscribe-nomail command to majordomo(at)postgresql(dot)org so that your
>message can get through to the mailing list cleanly
>
>


From: terry(at)greatgulfhomes(dot)com
To: "'Medi Montaseri'" <medi(dot)montaseri(at)intransa(dot)com>, <missive(at)hotmail(dot)com>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: newbie question for return date
Date: 2002-10-29 02:27:20
Message-ID: 002001c27ef2$b5a3ee00$2766f30a@development.greatgulfhomes.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

In order for your proposed result of array[-1] to be consistent/predictable,
an ORDER BY sorting action will have to be performed *anyway* (in order to
consistently return the last record(s) from the recordset), so doing an
ORDER BY field_name DESC LIMIT 1 will have the same effect.

Just my 2 cents

Terry Fielder
Network Engineer
Great Gulf Homes / Ashton Woods Homes
terry(at)greatgulfhomes(dot)com

> -----Original Message-----
> From: pgsql-general-owner(at)postgresql(dot)org
> [mailto:pgsql-general-owner(at)postgresql(dot)org]On Behalf Of Medi Montaseri
> Sent: Monday, October 28, 2002 8:13 PM
> To: missive(at)hotmail(dot)com
> Cc: pgsql-general(at)postgresql(dot)org
> Subject: Re: [GENERAL] newbie question for return date
>
>
> Hey a suggestion, what if PG would support the negative limit as in
> select * from table limit -1 to mean limit it from the other end of
> list.....
>
> Sort of like some languages where they support
>
> array[1] vs array[-1].
>
> I'm not sure, but it looks like order by will sort the list which is
> expensive and then
> allow us to get the first chunk specified by limit.
>
> Lee Harr wrote:
>
> >In article <xEgv9(dot)2668$h_4(dot)374526(at)news20(dot)bellglobal(dot)com>,
> tviardot wrote:
> >
> >
> >>Hi guys, here a newbies question.
> >>I've made a table with some action and date.
> >>How may i query the most recent date. (I'd like to return
> only the record
> >>which have the most recent date ).
> >>Tx
> >>
> >>
> >>
> >>
> >
> >How about:
> >
> >SELECT * FROM t ORDER BY d DESC LIMIT 1;
> >
> >
> >---------------------------(end of
> broadcast)---------------------------
> >TIP 3: if posting/reading through Usenet, please send an appropriate
> >subscribe-nomail command to majordomo(at)postgresql(dot)org so that your
> >message can get through to the mailing list cleanly
> >
> >
>
>
>
>
> ---------------------------(end of
> broadcast)---------------------------
> TIP 2: you can get off all lists at once with the unregister command
> (send "unregister YourEmailAddressHere" to
> majordomo(at)postgresql(dot)org)
>


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Medi Montaseri <medi(dot)montaseri(at)intransa(dot)com>
Cc: missive(at)hotmail(dot)com, pgsql-general(at)postgresql(dot)org
Subject: Re: newbie question for return date
Date: 2002-10-29 18:24:44
Message-ID: 22753.1035915884@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Medi Montaseri <medi(dot)montaseri(at)intransa(dot)com> writes:
> Hey a suggestion, what if PG would support the negative limit as in
> select * from table limit -1 to mean limit it from the other end of
> list.....

You'd hardly want that, as it would necessarily be the slowest possible
way of retrieving the rows you're after.

Instead, reverse the sort order. For example

select * from table order by datecol desc limit 1;

regards, tom lane


From: Richard Huxton <dev(at)archonet(dot)com>
To: "tviardot" <tviardot(at)sympatico(dot)ca>, pgsql-general(at)postgresql(dot)org
Subject: Re: newbie question for return date
Date: 2002-10-30 18:45:30
Message-ID: 200210301845.30758.dev@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Monday 28 Oct 2002 7:57 pm, tviardot wrote:
> Hi guys, here a newbies question.
> I've made a table with some action and date.
> How may i query the most recent date. (I'd like to return only the record
> which have the most recent date ).

SELECT * from table_name ORDER BY date_column DESC LIMIT 1;

If you have an index on date_column this will be almost instant. If two rows
have the same (largest) value in date_column which gets returned isn't well
defined.

--
Richard Huxton