oracle用rowid去掉重复值

oracle用rowid去掉重复值,第1张

你可以将这个 *** 作看做一个嵌套循环;
对test表的每一条记录,到test中找出所有a,b字段与当前字段一样的记录,找出其rowid最大值,跟当前记录的rowid比较,如果不等于则删除,如果相等,则保留。
所以,本条sql的功能是对所有a,b字段重复的记录,只保留一条。
就是说,本sql等价于下面的sql
delete from test t where rowid not in
(
select max(rowid) from test
group by a,b
)

1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断。

2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录。

3、查找表中多余的重复记录(多个字段)。

4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录。

5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录。就完成了。

代码如下:

create or replace function remove_rame_string(oldStr varchar2, sign varchar2)  
  return varchar2 is  
  
  /  
   Oracle去掉重复字符串  
   函数名称:RemoveSameStr  
   参    数:名称         类型       说明  
             oldStr           varchar2       要处理的字符串  
             sign             varchar2       字符串分隔符  
   返 回 值:Result           varchar2       不包含重复子串的记录  
  /  
  str          varchar2(2000);  
  currentIndex number;  
  startIndex   number;  
  endIndex     number;  
  
  type str_type is table of varchar2(30) index by binary_integer;  
  arr str_type;  
  Result varchar2(1000);  
begin  
  -- 空字符串  
  if oldStr is null then  
    return('');  
  end if;  
  
  --字符串太长  
  if length(oldStr) > 2000 then  
    return(oldStr);  
  end if;  
  str := oldStr;  
  
  currentIndex := 0;  
  startIndex   := 0;  
  
  loop  
    currentIndex := currentIndex + 1;  
    endIndex     := instr(str, sign, 1, currentIndex);  
    if (endIndex <= 0) then  
      exit;  
    end if;  
  
    arr(currentIndex) := trim(substr(str,  
                                     startIndex + 1,  
                                     endIndex - startIndex - 1));  
    startIndex := endIndex;  
  end loop;  
  
  --取最后一个字符串:  
  arr(currentIndex) := substr(str, startIndex + 1, length(str));  
  
  --去掉重复出现的字符串:  
  for i in 1  currentIndex - 1 loop  
    for j in i + 1  currentIndex loop  
      if arr(i) = arr(j) then  
        arr(j) := '';  
      end if;  
    end loop;  
  end loop;  
  
  str := '';  
  for i in 1  currentIndex loop  
    if arr(i) is not null then  
      str := str || sign || arr(i);  
      --数组置空:  
      arr(i) := '';  
    end if;  
  end loop;  
  
  --去掉前面的标识符:  
  Result := substr(str, 2, length(str));  
  
  return(Result);  
end remove_rame_string;


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/13366634.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-22
下一篇 2023-07-22

发表评论

登录后才能评论

评论列表(0条)

保存