SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 last record
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

srisha
Starting Member

India
38 Posts

Posted - 09/30/2013 :  06:13:17  Show Profile  Reply with Quote
show to get lase updatded record


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

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 09/30/2013 :  06:26:24  Show Profile  Reply with Quote
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
Starting Member

Germany
39 Posts

Posted - 10/07/2013 :  17:05:38  Show Profile  Reply with Quote
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

India
52309 Posts

Posted - 10/07/2013 :  23:33:58  Show Profile  Reply with Quote
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
Starting Member

Germany
39 Posts

Posted - 10/10/2013 :  19:29:30  Show Profile  Reply with Quote
Yes, you are right

M. Ncube
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000