【转帖】SQL Oracle删除重复记录
1.Oracle删除重复记录.
删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录.
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)
删除表中多余的重复记录(多个字段),只留有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)
================================
此方法可以适用于sql ,oracle
declare @max integer,@id integer
declare cur_rows cursor local for select 主字段,count(*) from 表名 group by 主字段 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 表名 where 主字段 = @id
/*
DECLARE @count INT
SELECT @count = COUNT(*) from [table1] WHERE [column1] = 1
DELETE TOP (@count-1) from [table1] WHERE [column1] = 1 这个top后面一定要有括号
*/
fetch cur_rows into @id,@max
end
close cur_rows
set rowcount 0
=======================================
select distinct * into #Tmp from tableName
drop table tableName
select * into tableName from #Tmp
drop table #Tmp
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)
=======================================
select identity(int,1,1) as id ,name,state into #tempTable from a
delete from a
delete from #tempTable
where id not in
(
select min(id) from #tempTable group by name
)
insert into a( name,state)
select name,state from #tempTable
drop table #tempTable
本文来自CSDN博客,出处:http://b
相关文档:
虽然学习Java很久了,自己也连接过一些数据库,比如mysql之类的,如今呢,也学习了一段时间的Oracle,然而呢,今天是我第一次连接Oracle,嘿嘿,应该还不算太迟吧。
今天呢,有点笨拙,大家莫笑!
我这是一个查询例子
首先,� ......
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
Go
----截取字符串,并出去html
create FUNCTION [dbo].[CutString] (@str varchar(1000),@length int)
RETURNS varchar(1000) AS
BEGIN
declare @mainstr varchar(1000)
declare @substr varchar(1000)
if(@str is not null or @st ......
DATEDIFF
返回跨两个指定日期的日期和时间边界数。
一、 语法
DATEDIFF ( datepart , startdate , enddate )
二、参数
datepar ......
一、适合读者对象
数据库开发程序员,数据库的数据量很多,涉及到对SP(存储过程)的优化的项目开发人员,对数据库有浓厚兴趣的人。
二、介绍
在数据库的开发过程中,经常会遇到复杂的业务逻辑和对数据库的操作,这个时候就会用SP来封装数据库操作。如果项目的SP较多,书写又没有一定的规
范,将会影响以后的系统维护困难 ......
PL/SQL DEVELOPER 基本用法详解(建议写过第一个存储过程后的初手必读)
用过oracle的都抱怨,为了稳定 它提供的图形化操作 速度慢的让人伤心呀,p4+128M的机器只要启动一个
oracle服务就够让人伤心的,再在dba studio里面操作真能让人哭。
pl/sql developer正是解决这个问题� ......