Oracle查詢和過濾重復數據


對數據庫某些意外情況,引起的重復數據,如何處理呢?

----------------查重復:

select *
  from satisfaction_survey s
 where s.as_side = 0
   and s.project_no in (select ss.project_no
                          from satisfaction_survey ss
                         where ss.as_side = 0
                         group by ss.project_no
                        having count(*) > 1)
 order by s.project_no, s.submit_time

思路:從預先選出的數據中(s.as_side = 0),根據特定字段(s.project_no),判斷是否存在(count(*) > 1,符合條件的數據不止一條)重復數據

當然,也可以根據多個字段(in 前面的字段,用個括號括起來,in select子句的返回字段對應改變)判斷是否存在重復數據

----------------------去重復:

delete from satisfaction_survey s
 where s.as_side = 0
 and(s.project_no/*, to_char(s.submit_time, 'yyyy-mm-dd')*/) in
       (select ss.project_no/*, to_char(ss.submit_time, 'yyyy-mm-dd') */
       from satisfaction_survey ss
       where ss.as_side=0//根據自己實際情況,確定子句(清楚每個sql的結果集很重要)
       group by ss.project_no/*, to_char(ss.submit_time, 'yyyy-mm-dd')*/
      having count(*) > 1)
   and rowid not in
       (select max(rowid) from satisfaction_survey where as_side=0 group by project_no/*, to_char(submit_time, 'yyyy-mm-dd')*/ having count(*) > 1)

思路:在刪除全部重復數據時,保留一條(and rowid not in)符合自己需求的數據

這里為了保證程序的健壯性,可以在查詢數據時防止查出多條數據,在返回結果外面加一層,只取一條數據:

select *
  from (SELECT ROW_NUMBER() OVER(ORDER BY s.submit_time) 插入時間, s.*
          FROM satisfaction_survey s
         where s.project_no = 'WW-PM-2017-RP-449'
           and s.employee_no = 'BF1019589'
           and s.as_side = 0
           --and s.mark is not null)
           and s.mark is null)
 where 插入時間 = 1

這里根據時間排序,取符合條件的第一條


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM