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;

SQLserver Delete from where 与Oracle delete from where 的差异的更多相关文章

  1. Oracle delete input与delete all input

    oracle官方文档提示:If you had specified DELETE INPUT rather than DELETE ALL INPUT, then RMAN would have on ...

  2. Oracle delete和truncate实践操作之一

    实践说明 本文章主要记录在Oracle中,delete和truncate进行数据删除之后,如何进行数据恢复.由于网上对delete和truncate的区别说明较多,此处不过多介绍两者区别. 注:由于环 ...

  3. Sqlserver通过链接服务器访问Oracle的那些事儿

    前言: 1.不经历风雨,怎能见彩虹. 2.充分利用BaiDu.google等搜索引擎查找资料并整合分析! 3.世上无难事只怕有心人! 本文由来:笔者在研究SQLSERVER链接服务器到oracle并使 ...

  4. mysql没有delete操作,那是delete from操作,

    1.mysql没有delete操作,那是delete from操作, 2.DELETE FROM table_name [WHERE Clause]

  5. C++ new operator, delete operator, operator new, operator delete, new placement

    http://www.younfor.com/cpp-new-placement-new-operator-new.html http://www.cnblogs.com/luxiaoxun/arch ...

  6. 【转】Sqlserver通过链接服务器访问Oracle的那些事儿!

    原文:http://blog.sina.com.cn/s/blog_614b6f210100t80r.html 前言:1.不经历风雨,怎能见彩虹.2.充分利用BaiDu.google等搜索引擎查找资料 ...

  7. Sqlserver通过链接服务器访问Oracle的解决办法

    转自http://blog.sina.com.cn/s/blog_614b6f210100t80r.html 一.创建sqlserver链接服务(sqlserver链接oracle)  首先sqlse ...

  8. C++中的new/delete与operator new/operator delete

    new operator/delete operator就是new和delete操作符,而operator new/operator delete是函数. new operator(1)调用opera ...

  9. C++ new和delete实现原理——new和delete最终调用malloc和free

    new和delete最终调用malloc和free,关于malloc和free实现原理参见这篇文章: http://blog.csdn.net/passion_wu128/article/detail ...

随机推荐

  1. 关于报错:'sharedApplication' is unavailable: not available on iOS (App Extension) - Use view controller based

    最近在看Extension相关知识的时候,自己写了个小demo 发现[UIApplication sharedApplication]这个方法敲不出来了, 总是报错:'sharedApplicatio ...

  2. Zend Studio 上 安装使用Aptana插件(html,css,js代码提示功能) .

    最近装了zend studio 9.0 用了段时间发现写html,css,js代码没提示,要开dreamwaver(对js代码提示也不好).就网上搜索了下,发现了Aptana插件,装上用了下,感觉不错 ...

  3. 使你的 Google Summer of Code 建议被接收的5个技巧

    本文翻译自:http://www.di.ens.fr/~baghdadi/TXT_blog/5_advices_to_get_your_proposal_accepted.lyx.html 本文讲的主 ...

  4. Redis中的批量删除数据库中的Key

    本文参考:http://blog.csdn.net/spring21st/article/details/15771861 http://stackoverflow.com/questions/575 ...

  5. document.execCommand()命令小计

    2D-Position 允许通过拖曳移动绝对定位的对象. AbsolutePosition 设定元素的 position 属性为“absolute”(绝对). BackColor 设置或获取当前选中区 ...

  6. MongoDB - The mongo Shell, mongo Shell Quick Reference

    mongo Shell Command History You can retrieve previous commands issued in the mongo shell with the up ...

  7. Android开发的十项注意

    随着移动平台的发展及其应用的不断改善,质量成为决定成败的关键.用户要求他们安装的应用响应快.性能好,如果某个应用不能提供卓越的功能和稳定的用户体验,那注定会被很快卸载: 尽管现在Android智能手机 ...

  8. Chrome浏览器跨域问题

    最近在学习Ionic,调试http请求的时候遇到跨域问题 解决办法:使用chrome浏览器安装这个插件:Allow-Control-Allow-Origin: *

  9. Android异步下载网络图片

    最近新做的一个项目,里面需要下载网络上的图片,并显示在UI界面上,学Android有个常识,就是Android中在主线程中没法直接更新UI的,要想更新UI必须另外开启一个线程来实现,当开启的线程完成图 ...

  10. MVC4 使用 ckfinder+ckeditor编辑器

    配置ckfinder for asp.net 版本下载地址  http://cksource.com/ckfinder/downloadckeditor下载地址 http://ckeditor.com ...