zoukankan      html  css  js  c++  java
  • sql脚本-维护一张表中的数据,按照保单号,将最近生成的数据删除掉,只保留之前生成的数据

    最近在项目中遇到这样一个问题

    运行sql语句:select a.contno from LIS_BUSI_TRANSACTIONS a where 1=1 and a.ruleid='16' group by a.contno having count(a.contno)>1

    这条sql语句的意思是能够查询出来有多少张保单是存在重复数据。

    现在的需求是将有重复的数据按照保单号进行分组,只要最先产生的那一条数据。

    现在要做的事,我要写一个sql脚本对这张表中的数据进行维护:

    思路:想:一条sql是肯定是办不成事的,需要借助于中间表,基本做法是:先将自己要保留的数据保存到中间表中,然后后将将有问题的数据删除掉,将中间表的数据保存到原表中,最后,再将借助的中间表删除掉就可以了,(其实中间表不删掉也是可以的,留一个备份也是不错的选择的,毕竟在实际项目中数据是不能随随便便的删除)。

    sql脚本如下:

    create table temp_lis_busi_transactions_a as
    ( SELECT min(sid) as sid,contno FROM lis_busi_transactions where invoiceflag = '00' and successflag = '1' and sum_total > 0 and contno in (select distinct contno from lis_busi_transactions where 1 = 1 and invoiceflag = '00' and successflag = '1' and sum_total > 0 group by contno having count(contno) > 1) /* and rownum=1*/ group by contno );

    create table temp_lis_busi_transactions_b as select * from lis_busi_transactions where 1=2;

    insert into temp_lis_busi_transactions_b
    select * from lis_busi_transactions where sid in(
    select sid from temp_lis_busi_transactions_a);

    delete from lis_busi_transactions where invoiceflag='00' and successflag='1' and sum_total>0;
    insert into lis_busi_transactions select * from temp_lis_busi_transactions_b;
    drop table temp_lis_busi_transactions_a;
    drop table temp_lis_busi_transactions_b;

  • 相关阅读:
    网站无脑搭建,自己可以建个站玩一玩儿
    Python2和Python3中urllib库中urlencode的使用注意事项
    图像的缩放与图像金字塔
    模糊聚类算法(FCM)
    java调用python脚本
    雷林鹏分享:jsp HTTP 状态码
    雷林鹏分享:jsp 服务器响应
    雷林鹏分享:jsp 客户端请求
    雷林鹏分享:jsp 隐式对象
    雷林鹏分享:jsp 动作元素
  • 原文地址:https://www.cnblogs.com/dongyaotou/p/12284136.html
Copyright © 2011-2022 走看看