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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 last record

Author  Topic 

srisha
Starting Member

38 Posts

Posted - 2013-09-30 : 06:13:17
show to get lase updatded record


--------------------------
BY
SRISHA

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-09-30 : 06:26:24
Assuming you've column called lastupdated you can use logic like

SELECT TOP 1 WITH TIES * FROM Table ORDER BY LastUpdated DESC


to get all records which have greatest value of last updated date

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

marcusn25
Yak Posting Veteran

56 Posts

Posted - 2013-10-07 : 17:05:38
Assuming you have [LastUpdateDate] date column from CustomerTable

SELECT
*
From
(
SELECT
Row_Number ()Over (Partition by CustomerID Order By LastUpdateDate desc) AS LastUpdate
,LastUpdateDate
,CustomerID
FROM Customer
) as C
Where
C.LastUpdateDate = 1

M. Ncube
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-10-07 : 23:33:58
quote:
Originally posted by marcusn25

Assuming you have [LastUpdateDate] date column from CustomerTable

SELECT
*
From
(
SELECT
Row_Number ()Over (Partition by CustomerID Order By LastUpdateDate desc) AS LastUpdate
,LastUpdateDate
,CustomerID
FROM Customer
) as C
Where
C.LastUpdateDate = 1

M. Ncube


This will not return all records if there are multiple ones existing for the same maximum LastUpdateDate value

this tweak will make it work though!

SELECT
*
From
(
SELECT
Dense_Rank ()Over (Partition by CustomerID Order By LastUpdateDate desc) AS LastUpdate
,LastUpdateDate
,CustomerID
FROM Customer
) as C
Where
C.LastUpdate = 1


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

marcusn25
Yak Posting Veteran

56 Posts

Posted - 2013-10-10 : 19:29:30
Yes, you are right

M. Ncube
Go to Top of Page
   

- Advertisement -