Home All Groups Group Topic Archive Search About

How do I find the oldest record in a table

Author
14 May 2005 8:57 AM
Kjell Arne Johansen
Hi

In one of my tables in the MS SQL database all records are time stamped.
I want to know what is the oldest record.

I have used SELECT TOP 1 * FROM <table> WHERE Time > May 12 1990...
-and it works because I know the data in the table is newer than 1990 but is
there a more intelligent way of doing it?
(I also think it work because the primary key has an ascending sorting
order).

Thanks for Your help.

Regards
Kjell Arne Johansen

Author
14 May 2005 9:18 AM
Itzik Ben-Gan
Is the time stamp unique in your table?

If so, use:

SELECT TOP 1 * FROM <table> ORDER BY ts DESC

Or

SELECT * FROM T1
WHERE ts = (SELECT MAX(ts) FROM T1)

If it's not unique, use:

SELECT TOP 1 * FROM <table> ORDER BY ts DESC, key DESC

Or

SELECT * FROM T1
WHERE key =
  (SELECT MAX(key) FROM T1
   WHERE ts = (SELECT MAX(ts) FROM T1))

--
BG, SQL Server MVP
www.SolidQualityLearning.com


Show quote
"Kjell Arne Johansen" <kjell***@online.no> wrote in message
news:E3jhe.10021$SL4.226180@news4.e.nsc.no...
> Hi
>
> In one of my tables in the MS SQL database all records are time stamped.
> I want to know what is the oldest record.
>
> I have used SELECT TOP 1 * FROM <table> WHERE Time > May 12 1990...
> -and it works because I know the data in the table is newer than 1990 but
> is there a more intelligent way of doing it?
> (I also think it work because the primary key has an ascending sorting
> order).
>
> Thanks for Your help.
>
> Regards
> Kjell Arne Johansen
>
>
Author
14 May 2005 10:58 AM
Kjell Arne Johansen
Thank You for your examples.
The time is not unique.  I will have to use a combination of time and two
other fields.


Regards
Kjell Arne

Show quote
"Itzik Ben-Gan" <it***@REMOVETHIS.SolidQualityLearning.com> skrev i melding
news:%23OzlmXGWFHA.3540@TK2MSFTNGP15.phx.gbl...
> Is the time stamp unique in your table?
>
> If so, use:
>
> SELECT TOP 1 * FROM <table> ORDER BY ts DESC
>
> Or
>
> SELECT * FROM T1
> WHERE ts = (SELECT MAX(ts) FROM T1)
>
> If it's not unique, use:
>
> SELECT TOP 1 * FROM <table> ORDER BY ts DESC, key DESC
>
> Or
>
> SELECT * FROM T1
> WHERE key =
>  (SELECT MAX(key) FROM T1
>   WHERE ts = (SELECT MAX(ts) FROM T1))
>
> --
> BG, SQL Server MVP
> www.SolidQualityLearning.com
>
>
> "Kjell Arne Johansen" <kjell***@online.no> wrote in message
> news:E3jhe.10021$SL4.226180@news4.e.nsc.no...
>> Hi
>>
>> In one of my tables in the MS SQL database all records are time stamped.
>> I want to know what is the oldest record.
>>
>> I have used SELECT TOP 1 * FROM <table> WHERE Time > May 12 1990...
>> -and it works because I know the data in the table is newer than 1990 but
>> is there a more intelligent way of doing it?
>> (I also think it work because the primary key has an ascending sorting
>> order).
>>
>> Thanks for Your help.
>>
>> Regards
>> Kjell Arne Johansen
>>
>>
>
>
Author
14 May 2005 2:27 PM
CBretana
Then order by all three columns, desc... and use Top 1

Show quote
"Kjell Arne Johansen" wrote:

> Thank You for your examples.
> The time is not unique.  I will have to use a combination of time and two
> other fields.
>
>
> Regards
> Kjell Arne
>
> "Itzik Ben-Gan" <it***@REMOVETHIS.SolidQualityLearning.com> skrev i melding
> news:%23OzlmXGWFHA.3540@TK2MSFTNGP15.phx.gbl...
> > Is the time stamp unique in your table?
> >
> > If so, use:
> >
> > SELECT TOP 1 * FROM <table> ORDER BY ts DESC
> >
> > Or
> >
> > SELECT * FROM T1
> > WHERE ts = (SELECT MAX(ts) FROM T1)
> >
> > If it's not unique, use:
> >
> > SELECT TOP 1 * FROM <table> ORDER BY ts DESC, key DESC
> >
> > Or
> >
> > SELECT * FROM T1
> > WHERE key =
> >  (SELECT MAX(key) FROM T1
> >   WHERE ts = (SELECT MAX(ts) FROM T1))
> >
> > --
> > BG, SQL Server MVP
> > www.SolidQualityLearning.com
> >
> >
> > "Kjell Arne Johansen" <kjell***@online.no> wrote in message
> > news:E3jhe.10021$SL4.226180@news4.e.nsc.no...
> >> Hi
> >>
> >> In one of my tables in the MS SQL database all records are time stamped.
> >> I want to know what is the oldest record.
> >>
> >> I have used SELECT TOP 1 * FROM <table> WHERE Time > May 12 1990...
> >> -and it works because I know the data in the table is newer than 1990 but
> >> is there a more intelligent way of doing it?
> >> (I also think it work because the primary key has an ascending sorting
> >> order).
> >>
> >> Thanks for Your help.
> >>
> >> Regards
> >> Kjell Arne Johansen
> >>
> >>
> >
> >
>
>
>

AddThis Social Bookmark Button