22,209
社区成员
发帖
与我相关
我的任务
分享
create table tb(item varchar(10),price int,comments nvarchar(20))
insert into tb select 'A',8,'3号买的'
insert into tb select 'B',2,'10号买的'
insert into tb select 'A',3,'15号买的'
insert into tb select 'B',6,'17号买的'
insert into tb select 'B',9,'20号买的'
insert into tb select 'A',1,'30号买的'
go
select * from tb a where not exists(select 1 from tb where item=a.item and price>a.price)
/*
item price comments
---------- ----------- --------------------
A 8 3号买的
B 9 20号买的
(2 行受影响)
*/
if object_id('[TB]') is not null drop table [TB]
go
create table [TB] (item nvarchar(2),price int,comments nvarchar(10))
insert into [TB]
select 'A',8,'3号买的' union all
select 'B',2,'10号买的' union all
select 'A',3,'15号买的' union all
select 'B',6,'17号买的' union all
select 'B',9,'20号买的' union all
select 'A',1,'30号买的'
select * from [TB]
SELECT item,price,comments FROM (
SELECT ROW_NUMBER()OVER (PARTITION BY item ORDER BY item,price DESC) AS NO ,*
FROM dbo.TB)T
WHERE no =1
/*
item price comments
A 8 3号买的
B 9 20号买的*/
select * from tb a where not exists(select 1 from tb where item=a.item and price>a.price)
select * from tb t where price=(select max(price) from tb where iteim=t.item)