34,873
社区成员
发帖
与我相关
我的任务
分享
DECLARE @t TABLE (id INT,tac VARCHAR(2),tbc VARCHAR(2))
INSERT INTO @t
SELECT 001,'a','b' UNION ALL
SELECT 001,'c','m' UNION ALL
SELECT 001,'a','c' UNION ALL
SELECT 002,'q','v' UNION ALL
SELECT 002,'q','v' union all
SELECT 003,'r','a'
select id,tac,tbc
from
(select *,row=row_number()over(partition by id order by id) from @t)t
where row=3
select
distinct *
from
tb t
where
not exists (select 1 from tb where item = t.item and oem_no < t.oem_no)create table tb(item varchar(10), oem varchar(10), oem_no varchar(10))
insert into tb values('0001' , 'a' , 'b')
insert into tb values('0001' , 'c' , 'm')
insert into tb values('0001' , 'a' , 'c')
insert into tb values('0002' , 'q' , 'v')
insert into tb values('0002' , 'q' , 'v')
insert into tb values('0003' , 'r' , 'a')
go
select distinct t.* from tb t where oem_no = (select min(oem_no) from tb where item = t.item)
select distinct t.* from tb t where not exists (select 1 from tb where item = t.item and oem_no < t.oem_no)
select distinct item,oem,oem_no from
(
select t.* , px = (select count(1) from tb where item = t.item and (oem_no < t.oem_no or (oem_no = t.oem_no and oem < t.oem) )) + 1 from tb t
) m where px = 1
drop table tb
/*
item oem oem_no
---------- ---------- ----------
0001 a b
0002 q v
0003 r a
(所影响的行数为 3 行)
*/SELECT DISTINCT * FROM TB T WHERE OEM=(SELECT MAX(OEM) FROM TB WHERE ITEM=T.ITEM)SELECT DISTINCT * FROM TB T WHERE OEM=(SELECT MIN(OEM) FROM TB WHERE ITEM=T.ITEM)select *
from
(select *,row=row_number()over(partition by item order by oem) from t1)t
where row=1