日期:2014-05-18 浏览次数:20830 次
declare @t table (ID varchar(3),CUSTOMER varchar(3),PRODUCTNUMBER varchar(6)) insert into @t select '001','C01','P00001' union all select '002','C01','P00001' union all select '003','C02','P00002' union all select '004','C02','P00002' select * from @t t where ID =(select min(ID) from @t where PRODUCTNUMBER=t.PRODUCTNUMBER) /* ID CUSTOMER PRODUCTNUMBER ---- -------- ------------- 001 C01 P00001 003 C02 P00002 */
------解决方案--------------------
select * from tb t where not exists(select 1 from tb where CUSTOMER=t.CUSTOMER and PRODUCTNUMBER=t.PRODUCTNUMBER and id<t.id)
------解决方案--------------------
select * from tb t where not exists (select 1 from tb where customer = t.customer and id > t.id)
------解决方案--------------------
+1
select * from tb t where not exists (select 1 from tb where customer = t.customer and id > t.id) and exists (select 1 from tb where customer = t.customer and id <> t.id)
------解决方案--------------------
use tempdb; /* create table A ( ID nvarchar(10) not null, CUSTOMER nvarchar(10) not null, PRODUCTNUMBER nvarchar(10) not null ); insert into A values ('001','C01','P00001'), ('002','C01','P00001'), ('003','C02','P00002'), ('004','C02','P00002'), ('005','C03','P00003'), ('006','C04','P00004'); */ select B.ID,B.CUSTOMER,B.PRODUCTNUMBER from ( select *,row_number() over(partition by A.CUSTOMER,A.PRODUCTNUMBER order by A.ID desc) as [orderno] from A ) as B where B.[orderno] = 1;
------解决方案--------------------
declare @t table (ID varchar(3),CUSTOMER varchar(3),PRODUCTNUMBER varchar(6)) insert into @t select '001','C01','P00001' union all select '002','C01','P00001' union all select '003','C02','P00002' union all select '004','C02','P00002' union all select '005','C03','P00003' union all select '006','C04','P00004' SELECT MIN(ID) FROM @T GROUP BY PRODUCTNUMBER HAVING COUNT(1)>1