简体   繁体   English

Access SQL 将最新记录追加到历史表

[英]Access SQL append latest records to historical table

I have this access SQL append query where I have a Primary key set to now allow duplicates in the destination table.我有这个访问 SQL 追加查询,其中我有一个主键设置现在允许在目标表中重复。 I'm confused if I am accomplishing the right thing here with the WHERE condition.如果我在这里用WHERE条件完成正确的事情,我很困惑。

I am trying to only screen the newest records from the source table 'tbl_IMEI_MASTER' and only append(add) the records that do not have a match on the key in the destination table (same identifier as the source table).我试图只筛选源表“tbl_IMEI_MASTER”中的最新记录,并且只追加(添加)与目标表中的键不匹配的记录(与源表相同的标识符)。 I think it's working but I did get a message that 72 rows (which is the total that were new and unique for the addition to the source table based on most recent date) were being updated in the destination table when only 14 of them should have been updated/added.我认为它正在工作,但我确实收到一条消息,当只有 14 行应该有已更新/添加。 Only 14 should have been identified as not having the same unique key.只有 14 个应该被识别为不具有相同的唯一密钥。

INSERT INTO leads_historical (Customer, LeadNumber, ImportDate)
SELECT DISTINCT tbl_IMEI_MASTER.Customer, tbl_IMEI_MASTER.LeadNumber, tbl_IMEI_MASTER.ImportDate
FROM tbl_IMEI_MASTER
WHERE tbl_IMEI_MASTER.ImportDate = (SELECT MAX(tbl_IMEI_MASTER.ImportDate) FROM tbl_IMEI_MASTER);

I got it -我知道了 -

Using just select to break it down further I drilled down to the desired result.使用仅选择进一步分解它,我深入研究了所需的结果。

SELECT DISTINCT tbl_IMEI_MASTER.Customer, tbl_IMEI_MASTER.LeadNumber, tbl_IMEI_MASTER.ImportDate
FROM tbl_IMEI_MASTER
WHERE tbl_IMEI_MASTER.ImportDate=(SELECT MAX(tbl_IMEI_MASTER.ImportDate) FROM tbl_IMEI_MASTER) AND NOT EXISTS (SELECT leads_historical.Customer FROM leads_historical WHERE leads_historical.Customer = tbl_IMEI_MASTER.Customer);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM