当前位置:网站首页>SQL query duplicate record
SQL query duplicate record
2022-06-26 09:28:00 【Great white bear_ BlankBear】
1、 Look up redundant duplicate records in the table , Duplicate records are based on a single field (peopleId) To judge
select * from people
where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
2、 Delete redundant duplicate records in the table , Duplicate records are based on a single field (peopleId) To judge , There is only rowid Minimum record
delete from people
where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )>1)
3、 Look up redundant duplicate records in the table ( Multiple fields )
select * from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
4、 Delete redundant duplicate records in the table ( Multiple fields ), There is only rowid Minimum record
delete from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)
5、 Look up redundant duplicate records in the table ( Multiple fields ), It doesn't contain rowid Minimum record
select * from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)
( Two )
For example
stay A There is a field in the table “name”,
And between different records “name” The values might be the same ,
Now we need to find out the relationship between the records in the table ,“name” Value has duplicate entries ;
Select Name,Count(*) From A Group By Name Having Count(*) > 1
If the gender is the same, it is as follows :
Select Name,sex,Count(*) From A Group By Name,sex Having Count(*) > 1
( 3、 ... and )
Method 1
declare @max integer,@id integer
declare cur_rows cursor local for select Main field ,count(*) from Table name group by Main field having count(*) >; 1
open cur_rows
fetch cur_rows into @id,@max
while @@fetch_status=0
begin
select @max = @max -1
set rowcount @max
delete from Table name where Main field = @id
fetch cur_rows into @id,@max
end
close cur_rows
set rowcount 0
Method 2
There are two meanings of duplicate records , One is a completely repeated record , That is, all the fields are duplicate records , Second, some records with duplicate key fields , such as Name Field repeat , Other fields are not necessarily repeated or can be ignored .
1、 For the first kind of repetition , It's easier to solve , Use
select distinct * from tableName
You can get a result set without duplicate records .
If the table needs to delete duplicate records ( Duplicate record retention 1 strip ), You can delete it as follows
select distinct * into #Tmp from tableName
drop table tableName
select * into tableName from #Tmp
drop table #Tmp
The reason for this duplication is that the table is not designed properly , Adding a unique index column can solve the problem .
2、 This kind of duplication problem usually requires that the first record in the duplicate record be kept , The operation method is as follows
Suppose there are duplicate fields as Name,Address, The only result set of these two fields is required
select identity(int,1,1) as autoID, * into #Tmp from tableName
select min(autoID) as autoID into #Tmp2 from #Tmp group by Name,autoID
select * from #Tmp where autoID in(select autoID from #tmp2)
the last one select taken Name,Address Result sets that don't repeat ( But one more autoID Field , When actually writing, you can write it in select Clause )
( Four ) Duplicate query
select * from tablename where id in (
select id from tablename
group by id
having count(id) > 1
)
边栏推荐
- How does flutter transfer parameters to the next page when switching pages?
- "One week's work on Analog Electronics" - integrated operational amplifier
- jz2440---使用uboot燒錄程序
- Super data processing operator helps you finish data processing quickly
- Jz2440--- using uboot burning program
- "One week's study of model electricity" - capacitor, triode, FET
- 板端电源硬件调试BUG
- "One week's data collection" - logic gate
- Merrill Lynch data technology expert team | application of recommendation of relevant contents in group system data retrieval
- Solutions for safety management and control at the operation site
猜你喜欢

There is a strong demand for enterprise level data integration services. How to find a breakthrough for optimization?

Merrill Lynch data tempoai is new!

《一周搞定数电》-逻辑门

《一周搞定模电》—集成运算放大器

"One week's solution to analog electricity" - power circuit

Runtimeerror: object has no attribute NMS error record when using detectron2

How to solve the problem that NVIDIA model cannot be viewed by inputting NVIDIA SMI and quickly view NVIDIA model information of computer graphics card

"One week's work on Analog Electronics" - Basic amplification circuit

GAN Inversion: A Survey

install realsense2: The following packages have unmet dependencies: libgtk-3-dev
随机推荐
Badge series 5: use of codecov
51 single chip microcomputer ROM and ram
3 big questions! Redis cache exceptions and handling scheme summary
"One week's work on Analog Electronics" - optocoupler and other components
Kubernetes cluster deployment (v1.23.5)
Thinkphp5 using the composer installation plug-in prompts that the PHP version is too high
Principle and application of single chip microcomputer -- Overview
《单片机原理及应用》——概述
"One week's data collection" -- combinational logic circuit
[Journal of Computer Aided Design & computer graphics] overview of research on pedestrian re recognition methods based on generated countermeasure network
Understanding of swing transformer
Construction practice of bank intelligent analysis and decision-making platform
全面解读!Golang中泛型的使用
《一周搞定模电》-光耦等元器件
《一周搞定数电》——编码器和译码器
Bug encountered in training detectron2: the test set cannot be evaluated during training
There is a strong demand for enterprise level data integration services. How to find a breakthrough for optimization?
Js--- get the data with the same key value in the object array to get a new array
Solve Django's if Version (1, 3, 3): raise improverlyconfigured ('mysqlclient 1.3.3 or new is required
3大问题!Redis缓存异常及处理方案总结