SQL 處理重複值
建立測試表
create table dups(id number,title_name varchar2(15),type varchar2(15),price number);
insert into dups values(1,'Book Title 5','children',15.00);
insert into dups values(2,'Book Title 3','biography',7.00);
insert into dups values(3,'Book Title 1','history',10.00);
insert into dups values(4,'Book Title 2','children',20.00);
insert into dups values(5,'Book Title 4','history',15.00);
insert into dups values(6,'Book Title 1','history',10.00);
insert into dups values(7,'Book Title 3','biography',7.00);
insert into dups values(8,'Book Title 1','history',10.00);
SQL> select * from dups;
ID TITLE_NAME TYPE PRICE
---------- --------------- --------------- ----------
1 Book Title 5 children 15
2 Book Title 3 biography 7
3 Book Title 1 history 10
4 Book Title 2 children 20
5 Book Title 4 history 15
6 Book Title 1 history 10
7 Book Title 3 biography 7
8 Book Title 1 history 10
已選擇8行。
列出重複值
SQL> select title_name,type,price from dups group by title_name,type,price
2 having count(*) > 1;
TITLE_NAME TYPE PRICE
--------------- --------------- ----------
Book Title 3 biography 7
Book Title 1 history 10
列出每一行和它的重複數
SQL> select title_name,type,price,count(*) as NumDups from dups
2 group by title_name,type,price
3 having count(*) >= 1
4 order by count(*) desc;
TITLE_NAME TYPE PRICE NUMDUPS
--------------- --------------- ---------- ----------
Book Title 1 history 10 3
Book Title 3 biography 7 2
Book Title 4 history 15 1
Book Title 2 children 20 1
Book Title 5 children 15 1
刪除適當位置的冗餘重複行
delete from dups
where id < (
select max(d.id) from dups d
where d.title_name = dups.title_name
and d.type = dups.type
and d.price = dups.price)
SQL> /
已刪除3行。
在oracle中可以使用ROWID偽列id也同樣可以刪除重複值
SQL> delete from dups
2 where rowid < (
3 select max(d.rowid) from dups d
4 where d.title_name = dups.title_name
5 and d.type = dups.type
6 and d.price = dups.price);
已刪除3行。
create table dups(id number,title_name varchar2(15),type varchar2(15),price number);
insert into dups values(1,'Book Title 5','children',15.00);
insert into dups values(2,'Book Title 3','biography',7.00);
insert into dups values(3,'Book Title 1','history',10.00);
insert into dups values(4,'Book Title 2','children',20.00);
insert into dups values(5,'Book Title 4','history',15.00);
insert into dups values(6,'Book Title 1','history',10.00);
insert into dups values(7,'Book Title 3','biography',7.00);
insert into dups values(8,'Book Title 1','history',10.00);
SQL> select * from dups;
ID TITLE_NAME TYPE PRICE
---------- --------------- --------------- ----------
1 Book Title 5 children 15
2 Book Title 3 biography 7
3 Book Title 1 history 10
4 Book Title 2 children 20
5 Book Title 4 history 15
6 Book Title 1 history 10
7 Book Title 3 biography 7
8 Book Title 1 history 10
已選擇8行。
列出重複值
SQL> select title_name,type,price from dups group by title_name,type,price
2 having count(*) > 1;
TITLE_NAME TYPE PRICE
--------------- --------------- ----------
Book Title 3 biography 7
Book Title 1 history 10
列出每一行和它的重複數
SQL> select title_name,type,price,count(*) as NumDups from dups
2 group by title_name,type,price
3 having count(*) >= 1
4 order by count(*) desc;
TITLE_NAME TYPE PRICE NUMDUPS
--------------- --------------- ---------- ----------
Book Title 1 history 10 3
Book Title 3 biography 7 2
Book Title 4 history 15 1
Book Title 2 children 20 1
Book Title 5 children 15 1
刪除適當位置的冗餘重複行
delete from dups
where id < (
select max(d.id) from dups d
where d.title_name = dups.title_name
and d.type = dups.type
and d.price = dups.price)
SQL> /
已刪除3行。
在oracle中可以使用ROWID偽列id也同樣可以刪除重複值
SQL> delete from dups
2 where rowid < (
3 select max(d.rowid) from dups d
4 where d.title_name = dups.title_name
5 and d.type = dups.type
6 and d.price = dups.price);
已刪除3行。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26506993/viewspace-1802969/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL 處理重複資料MySql
- Oracle 重複資料處理Oracle
- 重複提交,你是如何處理的?
- 巧用狀態值處理複雜的 TableViewView
- 資料處理之去除重複資料
- Android處理按鈕重複點選Android
- 海量資料處理_刪除重複行
- MySQL 處理插入過程中的主鍵唯一鍵重複值辦法MySql
- SQL查詢列(欄位)重複值及操作--整理SQL
- [Python] Pandas 對資料進行查詢、替換、篩選、排序、重複值和缺失值處理Python排序
- 刪除oracle重複值Oracle
- 處理網站中的重複內容問題網站
- 處理表重複記錄(查詢和刪除)
- MySQL中處理各種重複的一些方法MySql
- Android優雅地處理按鈕重複點選Android
- Spring Cloud Stream如何處理訊息重複消費?SpringCloud
- mq要如何處理訊息丟失、重複消費?MQ
- TYOI Day1 travel:Tree dp【處理重複走邊】
- sql重複插入問題SQL
- 求一條sql關於按分組重複值不顯示?SQL
- Mysql如何處理重複資料?讓你的效率更高效MySql
- SQL SERVER應用例項——處理表重複記錄(查詢和刪除)_整理貼4 (轉)SQLServer
- HTML id屬性值不能重複HTML
- Oracle SQL處理OracleSQL
- sql - distinct 去重複的用法SQL
- 【SQL 學習】去掉重複行SQL
- SQL重複記錄查詢SQL
- T-SQL 刪除重複資料SQLSQL
- UITableView複雜介面處理UIView
- javascript去掉陣列中重複的值JavaScript陣列
- 列組合資料去重複值
- 【SQL】Oracle SQL處理的流程SQLOracle
- 可重複使用的水處理顆粒有效地消除了BPA
- 影像處理或其他多媒體處理中的值溢位處理
- SQL Server資料庫中處理空值時常見問題SQLServer資料庫
- Python 影像處理 OpenCV (6):影像的閾值處理PythonOpenCV
- 【scikit-learn基礎】--『預處理』之 缺失值處理
- 同花色同值牌處理