Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
hi,I have a table:version item price1 a 1.002 a 1.203 a 1.301 b 2.002 b 2.20I need to create a query that will select the latest version price for each itemthe outcome should be like that:version item price3 a 1.302 b 2.20please help regards,aviv
James K
Master Smack Fu Yak Hacker
3873 Posts
Posted - 2013-07-13 : 17:48:12
use one of these
SELECT b.version, a.item, b.priceFROM 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;