0

我尝试了以下方法。它不插入任何新行,尽管也没有重复的行。为什么呢?我做错了什么?任何帮助表示赞赏

     INSERT INTO TEMP_8
     SELECT PASS_M, ENTRY_DT, EXIT_D, WORKED_HRS, 
            CONSECUTIVE_D = num -(SELECT max(num)
                                  FROM TEMP_7 B
                                  WHERE B.CONSEC <= A.CONSEC
                                        AND B.GAP = 1
                                        AND A.IC_N = B.IC_N) + 1
     FROM TEMP_7 A
     WHERE NOT EXISTS (SELECT *
                       FROM TEMP_8);  

更新:

PASS_M ENTRY_DT, EXIT_DT, WORKED_HRS  
peter 20/12/12 21/12/12 12  
Tom 20/12/12 21/12/12 10  
Tom 21/12/12 22/12/12 9  

这些不是重复的。当name,entry_dtexit_dt完全相同时重复。

4

2 回答 2

2
 INSERT INTO TEMP_8
 SELECT PASS_M, ENTRY_DT, EXIT_D, WORKED_HRS, 
        CONSECUTIVE_D = num -(SELECT max(num)
                              FROM TEMP_7 B
                              WHERE B.CONSEC <= A.CONSEC
                                    AND B.GAP = 1
                                    AND A.IC_N = B.IC_N) + 1
 FROM TEMP_7 A
 WHERE NOT EXISTS (SELECT *
                   FROM TEMP_8 B
                   WHERE B.PASS_M = A.PASS_M 
                   and B.ENTRY_DT = A.ENTRY_DT 
                   and B.EXIT_D = A.EXIT_D); 
于 2012-12-26T09:47:20.277 回答
1

您需要使用 select 中的数据过滤 Exists,如下所示:

 INSERT INTO TEMP_8
 SELECT PASS_M, ENTRY_DT, EXIT_DT, WORKED_HRS, 
        CONSECUTIVE_D = num -(SELECT max(num)
                              FROM TEMP_7 B
                              WHERE B.CONSEC <= A.CONSEC
                                    AND B.GAP = 1
                                    AND A.IC_N = B.IC_N) + 1
 FROM TEMP_7 A
 WHERE NOT EXISTS (SELECT 1
                   FROM TEMP_8 T
 WHERE A.PASS_M=T.PASS_M AND A.ENTRY_DT=T.ENTRY_DT AND A.EXIT_DT=T.EXIT_DT);
于 2012-12-26T21:08:32.717 回答