sql 查詢重復數據,刪除重復數據,過濾重復數據


select * from (
SELECT titleid,count(titleid) c
FROM [DragonGuoShi].[dbo].[ArticleInfo]
group by titleid,[CategoryCode]
having count(titleid)>1)as t
order by c


delete [DragonGuoShi].[dbo].[ArticleInfo] where ID not in (
SELECT max(ID) c
FROM [DragonGuoShi].[dbo].[ArticleInfo]
group by titleid ,[CategoryCode]
)

Sql查詢語句過濾重復的數據

情況一:表中存在完全重復的的數據,即所有字段內容都是相同的
 
create table #
(用戶ID int, 姓名 varchar(10), 年齡 int )
insert into #
select 111, '張三', 26 union all
select 222, '李四', 25 union all
select 333, '王五', 30 union all
select 111, '張三', 26

方法: select distinct * from #
 
 情況2:表中存在部分數據重復的字段,即 重復數據中至少有一個字段不重復
 
create table #
(用戶ID int, 姓名 varchar(10), 年齡 int, 日期 DateTime )
insert into #
select 111, '張三', 26 2010-02-23 union all
select 222, '李四', 25 2010-03-13 union all
select 333, '王五', 30 2011-03-25 union all
select 111, '張三', 26 2011-07-07
 
方法:--當兩條重,取日期大的一條 select * from t a where not exists (select 1 from t where a.用戶ID=用戶ID a.姓名=姓名 and 日期>a.日期)
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM