2013-10-09 54 views
2

我可以使用SQL將數據提取到新表中將兩個現有表中的任何重複項提取出來嗎?將SQL中的重複項提取到Access中的新表中

下表是從我的訪問前的ERP系統獲取的快照。

PRE 
CO   Otp Los His Whs Fac 
11889204 C20 22 66 1BA 1BA 
11889132 C23 22 66 1BA 1BA 
11888835 C20 23 66 1BA 1BA 
11887689 C20 22 66 1BA 1BA 
11884977 C20 22 22 1BA 1BA 
11884624 C20 22 99 1BA 1BA 

下表是我訪問後拍攝的快照。

POST 
CO   Otp Los His Whs Fac 
11889204 C20 22 66 1BA 1BA 
11889132 C23 22 66 1BA 1BA 
11888835 C20 23 66 1BA 1BA 
11882804 C20 22 77 1BA 1BA 
11882724 C20 22 77 1BA 1BA 
11882521 C20 22 77 1BA 1BA 
11882431 C20 22 22 1BA 1BA 
11881410 C20 22 22 1BA 1BA 
11881282 C20 22 22 1BA 1BA 

現在我想的是POST訪問表只顯示新的記錄,和SQL以去除在前置和後置表複製任何記錄,並創建一個新表並粘貼重複出現。

現在我正在學習SQL,但這已經略微超出了我的深度。任何人都知道這是可能的?

+0

對不起它不會讓我添加圖片說明如何使用表看起來... – DaymoClark

回答

2

這將插入匹配從您的前置和後置表中的行成newtable

INSERT INTO newtable (POSTCO,Otp, Los, His, Whs, Fac) 
SELECT POSTCO,Otp, Los, His, Whs, Fac FROM posttable 
WHERE EXISTS 
(SELECT POSTCO,Otp, Los, His, Whs, Fac FROM pretable 
    WHERE POSTCO = posttable.POSTCO AND Otp = posttable.Opt AND 
    Los = posttable.Los AND His = posttable.His AND 
    Whs = posttable.Whs AND Fac = posttable.Fac) 

然後去除重複行:

DELETE FROM posttable WHERE EXISTS 
(SELECT POSTCO,Otp, Los, His, Whs, Fac 
    FROM pretable 
    WHERE POSTCO = posttable.POSTCO AND Otp = posttable.Opt 
    AND Los = posttable.Los AND His = posttable.His 
    AND Whs = posttable.Whs AND Fac = posttable.Fac) 
+0

我改變了它適合我的領域和表....這看起來不錯嗎? INSERT INTO newtable的(CO,OTP,洛,他的,WHS,FAC) SELECT CO,OTP,洛,他的,WHS,的Fac FROM交 WHERE EXISTS (SELECT CO,OTP,洛,他的,WHS,的Fac FROM pre WHERE CO = post.CO AND Otp = post.Opt AND Los = post.Los AND His = post.His AND Whs = post.Whs AND Fac = post.Fac) 感謝您的幫助! ! – DaymoClark