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 2008 Forums
 Transact-SQL (2008)
 help with a query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

aviv.sages
Starting Member

2 Posts

Posted - 07/13/2013 :  16:15:28  Show Profile  Reply with Quote
hi,

I have a table:

version item price
1 a 1.00
2 a 1.20
3 a 1.30
1 b 2.00
2 b 2.20

I need to create a query that will select the latest version price for each item
the outcome should be like that:
version item price
3 a 1.30
2 b 2.20

please help

regards,
aviv

James K
Flowing Fount of Yak Knowledge

3578 Posts

Posted - 07/13/2013 :  17:48:12  Show Profile  Reply with Quote
use one of these
SELECT
	b.version,	
	a.item,
	b.price
FROM
	YourTable a
	CROSS APPLY
	(SELECT TOP (1) version, price
	 FROM YourTable b 
	 WHERE b.item = a.item
	 ORDER BY version DESC
	)b;
	
;WITH cte AS
(
	SELECT *,
		ROW_NUMBER() OVER (PARTITION BY item ORDER BY version DESC) N
	FROM YourTable
) SELECT version, item, price FROM cte 
  WHERE N = 1;

Edited by - James K on 07/13/2013 17:48:39
Go to Top of Page

aviv.sages
Starting Member

2 Posts

Posted - 07/17/2013 :  16:13:33  Show Profile  Reply with Quote
Hi James,

Thank you very much.

Aviv
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.06 seconds. Powered By: Snitz Forums 2000