select name, MAX(price) from products
GROUP BY name
select name, MAX(id) from products
GROUP BY name
CREATE TABLE #products (
id int,
name nvarchar(255),
price money
)
insert into #products
(
id,
name,
price
)
values
(0,'пиво',100),(1,'водка',200),(2,'пиво',150),(3,'пиво',100),(4,'спирт',1000)
SELECT
id,
name,
price
FROM (
SELECT id
,NAME
,price
,RANK() OVER (PARTITION BY NAME ORDER BY ID DESC) RNK
FROM #products ) X
where X.RNK=1
SELECT t.name, (SELECT p.price FROM products p WHERE p.id = t.max_id) AS last_price
FROM (SELECT name, max(id) AS max_id FROM products GROUP BY name) t