22,207
社区成员
发帖
与我相关
我的任务
分享
use Tempdb
go
--> -->
if not object_id(N'Tempdb..#1') is null
drop table #1
Go
Create table #1([优先度] int,[店铺CD] int,[通过仓库CD] int)
Insert #1
select 1,3,5 union all
select 2,3,206 union all
select 1,4,65 union all
select 2,4,206 union all
select 3,4,998
Go
SELECT [优先度],[店铺CD],[通过仓库CD] FROM (Select *,ROW_NUMBER()OVER(PARTITION BY [店铺CD] ORDER BY [优先度] DESC) AS RN from #1) AS a WHERE RN=1
/*
优先度 店铺CD 通过仓库CD
2 3 206
3 4 998*/
select a.店铺CD,a.优先度.a.通过仓库CD from tb a as join (
select 店铺CD,max(优先度) 优先度 from tb group by 店铺CD
) b on a.店铺CD=b.店铺CD and a.优先度=b.优先度
create table #t (优先度 int,店铺CD int,通过仓库CD int)
insert into #t
select 1,3,5 union all
select 2,3,206 union all
select 1,4,65 union all
select 2,4,206 union all
select 3,4,998
select *
from #t a
where not exists(select 1 from #t b where b.店铺CD=a.店铺CD and b.优先度>a.优先度)
/*
优先度 店铺CD 通过仓库CD
----------- ----------- -----------
2 3 206
3 4 998
(2 row(s) affected)
*/