Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 select most recent time from table

Author  Topic 

feejaz
Yak Posting Veteran

68 Posts

Posted - 2008-06-19 : 02:36:31
Hi,

I have to create the query in which I have to select complete row records according to the column "time" where the time is most recent entered.

there are columns "username", "bookname", "Time"
a asp 6/4/2008 6:02:00 AM
b sql 6/4/2008 6:04:00 AM
a php 6/4/2008 6:05:00 AM



Navi

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-19 : 02:41:55
SELECT * FROM TABLE WHERE Time=(SELECT MAX(Time) FROm Table)

OR

SELECT * FROM Table t
INNER JOIN (SELECT MAX(Time) AS MaxTime FROM Table)tmp
ON tmp.MaxTime=t.Time
Go to Top of Page

raky
Aged Yak Warrior

767 Posts

Posted - 2008-06-19 : 02:43:11
quote:
Originally posted by feejaz

Hi,

I have to create the query in which I have to select complete row records according to the column "time" where the time is most recent entered.

there are columns "username", "bookname", "Time"
a asp 6/4/2008 6:02:00 AM
b sql 6/4/2008 6:04:00 AM
a php 6/4/2008 6:05:00 AM



Navi



The question is not clear please specify how you want your output to be.

Any way if u want to get latest record from any table
use this

select top 1 * from table name order by time desc
Go to Top of Page

feejaz
Yak Posting Veteran

68 Posts

Posted - 2008-06-19 : 03:31:39
thanks for reply,
it is running for me, can you tell me If I want to get the record as per user also.

Navi
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-19 : 03:58:13
quote:
Originally posted by feejaz

thanks for reply,
it is running for me, can you tell me If I want to get the record as per user also.
Navi


didnt get that. record as per user?
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-06-19 : 04:17:11
SELECT TOP 1 * FROM Table ORDER BY Time DESC

SELECT UserName, BookName, Time
FROM (
SELECT UserName, BookName, Time, ROW_NUMBER() OVER (PARTITION BY UserName ORDER BY Time DESC) AS RecID
FROM Table
) AS d
WHERE RecID = 1


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -