|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How do I find the oldest record in a tableHi
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 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)) 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 > > 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 >> >> > > 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 > >> > >> > > > > > > > |
|||||||||||||||||||||||