ǰλã >> SQL >> SQL找出重?数据?留一
  ϸ

SQL找出重?数据?留一

ȶȣ149   ʱ䣺2016-05-05 11:38:25.0
SQL找出重?数据?留一?
用SQL?,删除掉重复项?留一?/div>

在几千条记录?存在?些相同的记录,如何能用SQL?,删除掉重复的?br>1、查找表??余的重?记录,重复?录是根据单个字?(peopleId)来判断
select * from people
where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)

2、删除表??余的重?记录,重复?录是根据单个字?(peopleId)来判断,只留有rowid?小的记录
delete from people
where   peopleName in (select peopleName    from people group by peopleName      having count(peopleName) > 1)
and   peopleId not in (select min(peopleId) from people group by peopleName     having count(peopleName)>1)

3、查找表??余的重?记录(??段)
select * from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)

4、删除表??余的重?记录(??段),只留有rowid?小的记录
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、查找表??余的重?记录(??段),不包含rowid?小的记录
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)   

6.消除??段的左边的??位:

update tableName set [Title]=Right([Title],(len([Title])-1)) where Title like '?'

7.消除??段的右边的??位:

update tableName set [Title]=left([Title],(len([Title])-1)) where Title like '%?

8.假删除表??余的重?记录(??段),不包含rowid?小的记录
update vitae set ispass=-1
where peopleId in (select peopleId from vitae group by peopleId

 

select *from r_people_report_bak ppp where 1=1and length(ppp.reporting_period)<=6and ppp.organization_code  || '_' ||to_char(to_date(ppp.reporting_period ,'YYYY-mm'),'yyyy') || '-' || to_char(to_date(ppp.reporting_period ,'YYYY-mm'),'mm')|| '_' || ppp.datilor_allin (select pp.organization_code || '_' || pp.aa || '_' || pp.datilor_all from (select p.people_id,p.organization_code,p.datilor_all,to_char(to_date(p.reporting_period ,'YYYY-mm'),'yyyy') || '-' || to_char(to_date(p.reporting_period ,'YYYY-mm'),'mm') aafrom r_people_report_bak p where  p.reporting_period is not null ) ppgroup by pp.aa ,pp.organization_code,pp.datilor_allhaving count(1) > 1 )

 

 

 

  ؽ