sqlserver中重复数据值只取一条的sql语句

前端之家收集整理的这篇文章主要介绍了sqlserver中重复数据值只取一条的sql语句前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

<div class="codetitle"><a style="CURSOR: pointer" data="32970" class="copybut" id="copybut32970" onclick="doCopy('code32970')"> 代码如下:

<div class="codebody" id="code32970">
--建立数据表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
)

原文链接:https://www.f2er.com/mssql/63816.html

猜你在找的MsSQL相关文章