SQLserver Delete from where 與Oracle delete from where 的差異


1.SQLserver 版本:

select @@version;

Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
Dec 28 2012 20:23:12
Copyright (c) Microsoft Corporation
Enterprise Edition: Core-based Licensing (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1) (Hypervisor)

 

2.需求場景,生產系統中的數據為刷卡記錄,存在重復的情況,現在需要刪除重復的數據。

 具體判別重復的方式為:同一卡號、同一消費金額、同一消費窗口、兩條消費記錄的時間差小於30秒則認為是重復的。樣例數據如下:

2012210856 9.00 2016-03-02 11:47:05.000 消費 后勤集團\飲食中心\桂香園餐廳新\二樓\黑椒雞柳飯 本專科生 7686
2012210856 9.00 2016-03-02 11:47:30.000 消費 后勤集團\飲食中心\桂香園餐廳新\二樓\黑椒雞柳飯 本專科生 7687
2012210856 9.00 2016-03-02 11:47:48.000 消費 后勤集團\飲食中心\桂香園餐廳新\二樓\黑椒雞柳飯 本專科生 7688

3.查詢重復記錄

select a.* from dbo.ODS_CCNU_zengx_distinct a inner join dbo.ODS_CCNU_zengx_distinct b
on a.smt_salaryno = b.smt_salaryno  --同一卡號
    and a.smt_transmoney=b.smt_transmoney --同一消費金額
    and a.smt_org_name = b.smt_org_name  --同一消費窗口
    and datediff(ss,a.smt_dealdatetime,b.smt_dealdatetime)>=0 
    and datediff(ss,a.smt_dealdatetime,b.smt_dealdatetime)<30 --時間間隔為30秒之內
    and a.rownum_distinct != b.rownum_distinct
order by  a.smt_salaryno,a.smt_dealdatetime  ; 

或者這樣

select a.* from [dbo].ODS_CCNU_zengx_distinct a
where exists( select 1 from [dbo].ODS_CCNU_zengx_distinct b
    where a.smt_salaryno = b.smt_salaryno  --同一卡號
    and a.smt_transmoney=b.smt_transmoney --同一消費金額
    and a.smt_org_name = b.smt_org_name  --同一消費窗口
    and datediff(ss,a.smt_dealdatetime,b.smt_dealdatetime)>=0 
    and datediff(ss,a.smt_dealdatetime,b.smt_dealdatetime)<30 --時間間隔為30秒之內
    and a.rownum_distinct != b.rownum_distinct
    )
order by a.smt_salaryno,a.smt_dealdatetime ;

刪除重復記錄,如果在oracle中可以這樣寫

delete from dbo.ODS_CCNU_zengx_distinct a
where exists( select 1 from dbo.ODS_CCNU_zengx_distinct b
    where a.smt_salaryno = b.smt_salaryno  --同一卡號
    and a.smt_transmoney=b.smt_transmoney --同一消費金額
    and a.smt_org_name = b.smt_org_name  --同一消費窗口
    and datediff(ss,a.smt_dealdatetime,b.smt_dealdatetime)>0 
    and datediff(ss,a.smt_dealdatetime,b.smt_dealdatetime)<30 --時間間隔為30秒之內
    and a.rownum_distinct != b.rownum_distinct
    );

但是SQLserver不支持這種寫法,反而支持連接的方式(oracle不支持inner join 的方式)

delete a from dbo.ODS_CCNU_zengx_distinct a inner join dbo.ODS_CCNU_zengx_distinct b
on a.smt_salaryno = b.smt_salaryno  --同一卡號
    and a.smt_transmoney=b.smt_transmoney --同一消費金額
    and a.smt_org_name = b.smt_org_name  --同一消費窗口
    and datediff(ss,a.smt_dealdatetime,b.smt_dealdatetime)>=0 
    and datediff(ss,a.smt_dealdatetime,b.smt_dealdatetime)<30 --時間間隔為30秒之內
    and a.rownum_distinct != b.rownum_distinct; 

 


免責聲明!

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



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