--查詢 Select Min(id) As id, zd1, zd2, zd3 From 表 Group By zd1, zd2, zd3
select id=min(id),zd1,zd2,zd3 from 表 group by zd1,zd2,zd3
--如果是要刪除重復數據 Delete From 表 Where id Not In (Select Min(id) From 表 Group By zd1, zd2, zd3)
select min(id) as id,zd1,zd2,zd3 from tb group by zd1,zd2,zd3
create table tb(id int,zd1 int, zd2 int,zd3 int) insert tb select 1 , 111 ,222 ,333 union select 2 , 111, 222 ,444 union select 3 , 555 , 222 , 333 union select 4 , 111 ,222 , 333 union select 5 , 666 , 777 ,888 union select 6 , 555 ,222, 333select id=min(id),zd1,zd2,zd3 from tb group by zd1,zd2,zd3 drop table tb /* id zd1 zd2 zd3 ----------- ----------- ----------- ----------- 1 111 222 333 2 111 222 444 3 555 222 333 5 666 777 888(4 row(s) affected) */
declare @table table(id int,zd1 varchar(10),zd2 varchar(10),zd3 varchar(10)) insert into @table select 1,'111','222','333' union all select 2,'111','222','444' union all select 3,'555','222','333' union all select 4,'111','222','333' union all select 5,'666','777','888' union all select 6,'555','222','333'select min(id)as id,zd1,zd2,zd3 from @table group by zd1,zd2,zd3 /* id zd1 zd2 zd3 ----------- ---------- ---------- ---------- 1 111 222 333 2 111 222 444 3 555 222 333 5 666 777 888(4 row(s) affected) */
Select Min(id) As id, zd1, zd2, zd3 From 表 Group By zd1, zd2, zd3
select id=min(id),zd1,zd2,zd3
from 表
group by zd1,zd2,zd3
Delete From 表 Where id Not In (Select Min(id) From 表 Group By zd1, zd2, zd3)
insert tb
select 1 , 111 ,222 ,333
union select 2 , 111, 222 ,444
union select 3 , 555 , 222 , 333
union select 4 , 111 ,222 , 333
union select 5 , 666 , 777 ,888
union select 6 , 555 ,222, 333select id=min(id),zd1,zd2,zd3
from tb
group by zd1,zd2,zd3
drop table tb
/*
id zd1 zd2 zd3
----------- ----------- ----------- -----------
1 111 222 333
2 111 222 444
3 555 222 333
5 666 777 888(4 row(s) affected)
*/
insert into @table
select 1,'111','222','333'
union all select 2,'111','222','444'
union all select 3,'555','222','333'
union all select 4,'111','222','333'
union all select 5,'666','777','888'
union all select 6,'555','222','333'select min(id)as id,zd1,zd2,zd3 from @table group by zd1,zd2,zd3
/*
id zd1 zd2 zd3
----------- ---------- ---------- ----------
1 111 222 333
2 111 222 444
3 555 222 333
5 666 777 888(4 row(s) affected)
*/