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
 General SQL Server Forums
 New to SQL Server Programming
 How to retrieve MAX record
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

vijays3
Constraint Violating Yak Guru

India
343 Posts

Posted - 07/09/2013 :  07:56:26  Show Profile  Reply with Quote
Hi ,

I have retrieved the data using CTE . But still need the retrieve the
latest row number record from my result.



;with cte as 
(

Select ROW_NUMBER() over ( PARTITION by [T7S1_PRODUCT_CDE_original],[T7S1_BR_NO_original]  order by [sql_updated] ) rn , [sql_updated]
     
      ,[T7S1_TYP_CDE]
   
      ,[T7S1_PRODUCT_CDE_original]
 
      ,[T7S1_BR_NO_original] from [interface_i084].[dbo].[tb_i084_ds_CeresTLStageFileDocDetail](nolock)
      
)


Select * from cte 

My Query result:

rn	sql_updated	T7S1_TYP_CDE	T7S1_PRODUCT_CDE_original	T7S1_BR_NO_original
1	2012-06-26 00:17:32.007	703	A0200030OOO    	00066
2	2012-06-27 12:30:21.803	703	A0200030OOO    	00066
3	2012-07-13 01:15:36.073	703	A0200030OOO    	00066
1	2012-06-27 12:45:30.653	703	A0200030OOO    	00083
1	2012-06-25 07:45:33.907	703	A0200030OOO    	00090
2	2012-08-16 12:45:22.227	703	A0200030OOO    	00090

Now Expecting:


rn	sql_updated	T7S1_TYP_CDE	T7S1_PRODUCT_CDE_original	T7S1_BR_NO_original
3	2012-07-13 01:15:36.073	703	A0200030OOO    	00066
1	2012-06-27 12:45:30.653	703	A0200030OOO    	00083
2	2012-08-16 12:45:22.227	703	A0200030OOO    	00090


I want to retrieve the MAX rn  record.. Please suggest





Vijay is here to learn something from you guys.

James K
Flowing Fount of Yak Knowledge

3661 Posts

Posted - 07/09/2013 :  08:10:25  Show Profile  Reply with Quote
Add a where clause to your final select to pick only RN=1. Also, if you want the latest record, change the order by clause to descending.
;with cte as 
(

Select ROW_NUMBER() over ( PARTITION by [T7S1_PRODUCT_CDE_original],[T7S1_BR_NO_original]  order by [sql_updated] DESC ) rn , [sql_updated]
     
      ,[T7S1_TYP_CDE]
   
      ,[T7S1_PRODUCT_CDE_original]
 
      ,[T7S1_BR_NO_original] from [interface_i084].[dbo].[tb_i084_ds_CeresTLStageFileDocDetail](nolock)
      
)


Select * from cte WHERE rn=1
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