34,587
社区成员
发帖
与我相关
我的任务
分享
/*
Category Name Value Number
分类1 A 1 A-001
分类1 A 2 A-002
分类1 A 3 A-102
分类1 B 1 A-082
分类1 A 2 A-003
分类1 A 3 A-002
分类1 A 1 A-002
*/
/*
Category Name Value Number
分类1 A 1 A-001
分类1 A 1 A-002
分类1 A 2 A-003
分类1 B 1 A-082
分类1 A 3 A-102
*/
--> 测试数据:[test]
if object_id('[test]') is not null
drop table [test]
go
create table [test](
[Category] varchar(5),
[Name] varchar(1),
[Value] int,
[Number] varchar(5)
)
go
insert [test]
select '分类1','A',1,'A-001' union all
select '分类1','A',2,'A-002' union all
select '分类1','A',3,'A-102' union all
select '分类1','B',1,'A-082' union all
select '分类1','A',2,'A-003' union all
select '分类1','A',3,'A-002' union all
select '分类1','A',1,'A-002'
go
select
[Category],
[Name],
[Value],
[Number]
from
(
select
px=ROW_NUMBER()over(partition by [Number],[Name],[Category] order by [Value]),*
from
test
)t
where
px=1
/*
Category Name Value Number
分类1 A 1 A-001
分类1 A 1 A-002
分类1 A 2 A-003
分类1 B 1 A-082
分类1 A 3 A-102
*/
select Category,[Name],[Value],Number from t
where t.[Value]=(select min([Value]) from t t1
where t.Category=t1.Category and t.[Name]=t1.[Name] and t.[Number]=t1.[Number])
order by t.Number
--t为你的表