oracle去重等基礎問題

2021-09-08 22:23:54 字數 1760 閱讀 7597

--去重查詢方法一:根據id

select * from sxe where id in(select min(id) from sxe group by username) order by id asc;

--去重查詢方法二:根據rownum

select * from (select s.*,rownum rn from sxe s ) x where x.rn in (select min(rownum) from sxe group by username) order by id asc;

--去重查詢方法三:根據rowid

select * from (select s.*,rowid rid from sxe s) x where x.rid in (select min(rowid) from sxe group by username) order by id asc;

select s.*,rowid from sxe s where rowid in (select min(rowid) from sxe group by username) order by id asc;

--去重刪除方法一:根據id

delete from sxe where id not in (select min(id) from sxe group by username);

--去重刪除方法二:根據rownum

--delete from (select s.*,rownum from sxe s) x where rownum not in (select min(rownum) from sxe group by username);

--去重刪除方法三:根據rowid

delete from sxe where rowid not in (select min(rowid) from sxe group by username);

關於資料遷移,如何處理大資料量重複問題?針對oracle

create table table1 selet * from table2; --按照table2的結構建立table1,並將table2的資料匯入table1;

create table table1 select * from table2 where 1 = 2; --按照table2的結構建立table1,但不匯入資料;

開發過程中,如果涉及的資料量小的情況下刪除可以用簡單的sql執行。但是資料量很大的遷移,百萬千萬級的資料量,效能是瓶頸的發生點;

因為查詢需要時間,執行刪除需要時間,刪除完畢執行事務需要時間,因此效能基本上為零,弄不好資料庫假死,甚至電腦假死。

所我的大資料遷移經驗就是:

create table temp_table select * from table2 where id not in (select min(id) from table1 group by coln) order by coln asc;

drop table table2;

rename temp_table to table2;

增加列:

alter table table1 add column_name column_type;

修改列大小:

alter table table1 modify column_name new_column_type;

修改列名稱:

alter table table1 rename column cln1 to cln2;

刪除列:

alter table tabl1 drop column cln1;

Oracle 資料去重

假設資料表a,3個字段 mid 表id bjsj 報警時間 val 資料值 篩選出2019 09 20 2019 09 25時間段內記錄,每塊表的最後一條報警記錄。做法 按條件查詢出符合條件記錄,然後取每塊表的報警時間為最大的一條記錄。oracle資料庫提供了乙個函式 row number 用於給資...

oracle分頁與去重

查詢emp表中的記錄 分頁,每一頁顯示5條記錄 查詢第二頁的資料 select from select ename,sal,deptno,rownum rw from emp where rw 5 and rw 10 查詢第二頁的資料,並排序 將重覆記錄保留一條 如上,test2表中有4條重複資料,...

oracle 簡單去重過程

oracle利用rowid刪除表中重覆記錄 先看錶myemp 查出有重複資料的記錄 查出沒有重複資料的記錄 查出不重複的記錄 或者select from myemp e where rowid select max rowid from myemp e2 where e.userid e2.user...