SQL Update首次出现重复

时间:2016-01-04 11:45:12

标签: sql-server sql-update duplicates

我有一个数据库,它是从一个实用程序中填充的,它不能很好地处理同时请求。

在这些表中插入行的逻辑是有缺陷的,这允许派生表中的重复。我的目标是修复那些重复。

有两种类型的重复项:

  1. 如果密钥是重复的,但我们可以使用数字来告诉他们 开
  2. Key和Number都是重复的,在这种情况下我 想要更新第一次出现。
  3. 情况如何出现

    #history_table{
      display:block;
      position:relative;
    
      background-color:#CCCCCC;
      border:2px solid black;
      margin-left: initial;
      /*width:9px;*/
    
      width:100%;
    
     table-layout: fixed; /* Include this code */
    
    
    }
    
    #history_table td{  
      display:block;
    }
    

    这导致以下表格:

    /** Sequence of events **/
    
    
    /** First Set (Correct) **/
    
    Insert into Base values(100,'Base1');
        /** Find the biggest IKey in Base Table **/
        /** Set that as IKey in Derived Table (IKey = 1)**/
    Insert into Derived values(1,100,'Derived1');
    
    Insert into Base values(101,'Base2');
        /** Find the biggest IKey in Base Table **/
        /** Set that as IKey in Derived Table (IKey = 2)**/
    Insert into Derived values(2,101,'Derived2');
    
    
    
    /** Second Set ( duplicate IKey )**/
    
    Insert into Base values(200,'Base3');
        /** Find the biggest IKey in Base Table **/
        /** We would have gotten 3, but there was another insert before this query could be run **/
    Insert into Base values(201,'Base4');
        /** Because of the insert, we got a 4 instead of a 3 **/
    
    
        /** Set that as IKey in Derived Table (IKey = 4)**/
    Insert into Derived values(4,200,'Derived3');
        /** Find the biggest IKey in Base Table **/
        /** Set that as IKey in Derived Table (IKey = 4)**/
    Insert into Derived values(4,201,'Derived4');
    
    
    
    
    /** Third Set ( duplicate IKey and Number )**/
    
    Insert into Base values(300,'Base5');
        /** Find the biggest IKey in Base Table **/
        /** We would have gotten 5, but there was another insert before this query could be run **/
        /** This even had the "Number" column same as previous insert **/ 
    
    Insert into Base values(300,'Base6');
        /** Because of the insert, we got a 6 instead of a 5 **/
        /** Set that as IKey in Derived Table (IKey = 6)**/
    
    Insert into Derived values(6,300,'Derived5');
        /** Find the biggest IKey in Base Table **/
        /** Set that as IKey in Derived Table (IKey = 6)**/
    Insert into Derived values(6,300,'Derived6');
    

    派生

    IKey Number Base
    1   100     Base1
    2   101     Base2
    3   200     Base3
    4   201     Base4
    5   300     Base5
    6   300     Base6
    

    我已经能够找到案例#1的解决方案。

    IKey Number Derived
    1   100     Derived1
    2   101     Derived2
    4   200     Derived3
    4   201     Derived4
    6   300     Derived5
    6   300     Derived6
    

    我想找到#2的解决方案。

    这是一个用于试验问题的工作空间。  http://sqlfiddle.com/#!3/e5f4e/4

0 个答案:

没有答案