Home > Database > Mysql Tutorial > sqlserver中重复数据值只取一条的sql语句

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

WBOY
Release: 2016-06-07 18:05:39
Original
1601 people have browsed it

sqlserver中有时候我们需要获取多条重复数据的一条,需要的朋友可以参考下面的语句

代码如下:
--建立数据表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 @ibegin
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
)
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template