sqlserver中重复数据值只取一条的sql语句
程序员文章站
2023-12-02 21:02:40
复制代码 代码如下: --建立数据表createtable testdata ( id int identity(1,1) primary key, data int, c...
复制代码 代码如下:
--建立数据表createtable testdata
(
id int identity(1,1) primary key,
data int,
cola varchar(20),
colb varchar(20)
)
go
--插入测试数据
declare @counts int
declare @i int
set @counts = 10000
set @i = 1
while @i<=@counts
begin
insert testdata (data,cola,colb) values(cast(rand()*10000 as int),cast(rand() as varchar(20)),cast(rand() as varchar(20)))
set @i=@i+1
end
--获取数据(重复数据只取一条)
select * from testdata
where
id in
(
--根据data分类获取数据最小id列表
select min(id) from testdata
group by data
)