select a.* from 表 a where not exists(select 1 from 表 where 型号=a.型号 and 规格=a.规格 and 列别>a.列别)
若这两个信息相同的话,其他的信息是否也相同 --------------------- 若相同的话, select 型号,规格,max(名称),max(列别) from tb group by 型号,规格 ---------------------- 若不相同 select 型号,规格,名称,列别 from tb a where 名称+':'+列别 in (select top 1 名称+':'+列别 from tb where 型号=a.型号 and 规格=a.规格)
select a.* from 表 a where not exists(select 1 from 表 where 型号=a.型号 and 规格=a.规格 and 列别>a.列别)这个可以,用discint不能实现吗 列别>a.列别 是什么意思啊?
discint不可以的declare @t table(id int,col1 char(2),col2 char(2),col3 char(2)) insert into @t select 1,'01','01','aa' union all select 2,'01','01','bb' union all select 3,'01','02','cc' union all select 4,'01','02','dd' select * from @tselect a.* from @t a where not exists(select 1 from @t where col1=a.col1 and col2=a.col2 and id>a.id)/*(所影响的行数为 4 行)id col1 col2 col3 ----------- ---- ---- ---- 1 01 01 aa 2 01 01 bb 3 01 02 cc 4 01 02 dd(所影响的行数为 4 行)id col1 col2 col3 ----------- ---- ---- ---- 2 01 01 bb 4 01 02 dd*/
declare @t table(id int,col1 char(2),col2 char(2),col3 char(2)) insert into @t select 1,'01','01','aa' union all select 2,'01','01','bb' union all select 3,'01','02','cc' union all select 4,'01','02','dd' select * from @tselect a.* from @t a where not exists(select * from @t where col1=a.col1 and col2=a.col2 and id<a.id) /* (所影响的行数为 4 行)id col1 col2 col3 ----------- ---- ---- ---- 1 01 01 aa 2 01 01 bb 3 01 02 cc 4 01 02 dd(所影响的行数为 4 行)id col1 col2 col3 ----------- ---- ---- ---- 1 01 01 aa 3 01 02 cc(所影响的行数为 2 行)*/
---------------------
若相同的话,
select 型号,规格,max(名称),max(列别)
from tb
group by 型号,规格
----------------------
若不相同
select 型号,规格,名称,列别
from tb a
where 名称+':'+列别 in (select top 1 名称+':'+列别
from tb
where 型号=a.型号 and 规格=a.规格)
列别>a.列别 是什么意思啊?
insert into @t
select 1,'01','01','aa' union all
select 2,'01','01','bb' union all
select 3,'01','02','cc' union all
select 4,'01','02','dd'
select * from @tselect a.* from @t a
where not exists(select 1 from @t where col1=a.col1 and col2=a.col2 and id>a.id)/*(所影响的行数为 4 行)id col1 col2 col3
----------- ---- ---- ----
1 01 01 aa
2 01 01 bb
3 01 02 cc
4 01 02 dd(所影响的行数为 4 行)id col1 col2 col3
----------- ---- ---- ----
2 01 01 bb
4 01 02 dd*/
insert into @t
select 1,'01','01','aa' union all
select 2,'01','01','bb' union all
select 3,'01','02','cc' union all
select 4,'01','02','dd'
select * from @tselect a.* from @t a
where not exists(select * from @t where col1=a.col1 and col2=a.col2 and id<a.id)
/*
(所影响的行数为 4 行)id col1 col2 col3
----------- ---- ---- ----
1 01 01 aa
2 01 01 bb
3 01 02 cc
4 01 02 dd(所影响的行数为 4 行)id col1 col2 col3
----------- ---- ---- ----
1 01 01 aa
3 01 02 cc(所影响的行数为 2 行)*/