Author |
Topic |
shahid09
Starting Member
35 Posts |
Posted - 2008-10-14 : 16:53:22
|
Hi All,My query returns 3 records but i want one record where timestamp is greater than other one.( means has max timestamp value).Sno FirstName LastName UserName timestamp1 Fred Flinstone freddo 2007-05-02 07.33.25.7987142 Homer Simpson homey 2007-05-02-13.42.50.3783563 Homer Brown notsofamous 2007-05-02-13.36.55.468393Can i use ORDER BY TIMESTAMP MAX ?Please Let me know if any one has suggestion.Regards,Shahid |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2008-10-14 : 16:59:54
|
maybe:select top 1 Sno, Firstname, ...from yourtablewhere ...order by timestamp DESCWebfredPlanning replaces chance by mistake |
|
|
shahid09
Starting Member
35 Posts |
Posted - 2008-10-14 : 18:02:23
|
Thanks Webfred,When i use Select top 1 i am getting an error. If i remove top 1 i get all three records and latest time first but when i use top 1 error is being generated.Thanks,Shahid |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-15 : 04:07:21
|
If using sql 2005 then use this. this will even return you all records if you've more than existing for same max valueSELECT TOP (1) WITH TIES *FROM(Your Query)tORDER BY t.timestamp DESC |
|
|
shahid09
Starting Member
35 Posts |
Posted - 2008-10-15 : 11:41:47
|
select top 1 Sno,Firstname, from tablewhere firstname = 'Homer'order by timestamp DESCAnd I got following errorAn Expected token "1" was found following "" Expected token may include : "+ - as <Identifier> SQLState = 42601When i remove top 1 i get two rows where firstname = HomerThanks,Shahid |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2008-10-15 : 11:45:11
|
EXTRA "," at the end of "Firstname"FIXED:select top 1Sno,Firstnamefrom tablewhere firstname = 'Homer'order by timestamp DESC |
|
|
shahid09
Starting Member
35 Posts |
Posted - 2008-10-15 : 12:10:26
|
Sorry i typed it wrong hereIt is actually select top 1Sno,Firstnamefrom tablewhere firstname = 'Homer'order by timestamp DESC But still getting same error.Thanks,Shahid |
|
|
shahid09
Starting Member
35 Posts |
Posted - 2008-10-15 : 12:24:56
|
One more thing we are using DB2 database |
|
|
shahid09
Starting Member
35 Posts |
Posted - 2008-10-15 : 12:36:06
|
I got it I think for DB2 we need to use FETCH FIRST 1 ROWS ONLYselect Sno,Firstnamefrom tablewhere firstname = 'Homer'order by timestamp DESC FETCH FIRST 1 ROWS ONL |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
hanbingl
Aged Yak Warrior
652 Posts |
|
|