简体   繁体   English

MS Access 2007更新/插入或合并或合并

[英]MS Access 2007 update/insert or merge or union

I have been searching for quite a while for how to do something that should be pretty easy to do in MS Access 2007. 我一直在寻找如何在MS Access 2007中做应该很容易的事情。

I have a main table, items_moved, that tracks the number of files received, by type, by day. 我有一个主表items_moved,该表按类型,按天跟踪收到的文件数。 The basic structure is: 基本结构是:

ID (autonumber), drive (text), type (text), date (DateTime), file_count (Number), file_size (Number) ID(自动编号),驱动器(文本),类型(文本),日期(DateTime),file_count(数字),file_size(数字)

Typical data looks like: 典型数据如下:

1777, F:\snaps, pics, 6/09/2010, 66, 151616131
1778, F:\snaps, pics, 6/10/2010, 5, 464864
1779, G:\pics, pics, 6/09/2010, 58, 45645646
1780, G:\pics, pics, 6/10/2010, 70, 123456667

I have a temp table with the exact same data structure. 我有一个具有完全相同的数据结构的临时表。 The temp table is generated by starting with (and including) the last day of the items_moved table and finding what's new. 临时表是通过从items_moved表的最后一天开始(包括该天)开始,然后查找新内容而生成的。

Typical data looks like: 典型数据如下:

1, F:\snaps, pics, 6/10/2010, 366, 6531616131
2, F:\snaps, pics, 6/11/2010, 5, 464864
3, G:\pics, pics, 6/10/2010, 70, 123456667
4, G:\pics, pics, 6/11/2010, 56, 123645964

All I'm trying to do is append the temp table on to the main table so that: 我要做的就是将临时表追加到主表上,以便:

  1. There are no duplicates (based on drive and date) 没有重复项(基于驱动器和日期)
  2. If the temp table has matching drive and date and larger values it overwrites the main table 如果临时表具有匹配的驱动器和日期以及更大的值,它将覆盖主表
  3. If the row exists in the temp table but not the main table, it gets appended to the end of the main . 如果该行存在于临时表中,但不存在于主表中,则该行将追加到main的末尾。

The result would be in the items_moved table which would records 1-1776 unchanged, but now end like this: 结果将在items_moved表中,该表将记录1-1776不变,但现在这样结束:

1777, F:\snaps, pics, 6/09/2010, 66, 151616131         (Unchanged)
1778, F:\snaps, pics, 6/10/2010, 366, 6531616131       (Updated temp was larger)
1779, G:\pics, pics, 6/09/2010, 58, 45645646           (unchanged)
1780, G:\pics, pics, 6/10/2010, 70, 123456667          (unchanged)
1781, F:\snaps, pics, 6/11/2010, 5, 464864             (added)
1782, G:\pics, pics, 6/11/2010, 56, 123645964          (added)

I've tried every variation on join that I can think of. 我已经尝试过各种可以想到的连接变体。

I can get the rows with matching dates, so I can use that for an update statement. 我可以获取具有匹配日期的行,因此可以将其用于更新语句。 I can't get the rows from the temp that don't have matching dates in the items_moved table, so I can't run the insert. 我无法从临时表中获得在items_moved表中没有匹配日期的行,因此无法运行插入。

The final point is to execute this as part of a VB action when the user requests a report from the access db. 最后一点是,当用户从访问数据库请求报告时,将其作为VB操作的一部分执行。

Here's a snippet 这是一个片段

INSERT INTO items_moved (drive, type, file_date, file_count, file_size) 
 SELECT 'F:\snaps','pics', temp_table.created, Count(temp_table.created),
  Sum(temp_table.size) FROM temp_table where temp_table.drive = 'F:\snaps' 
  GROUP BY temp_table.created

This works, but just adds to the end of the table. 这可行,但只是添加到表的末尾。 I'm left with the problem of duplicates. 我剩下重复的问题。 I'm sure there's a way to de-duplicate the table on the fly, but that seems like a waste. 我敢肯定有一种方法可以动态删除表中的重复数据,但这似乎是一种浪费。 I've found some hints on using union , but nothing on how to use union to update one of the tables in the union. 我发现了有关使用union的一些提示,但是没有关于如何使用union更新联盟中的表之一的任何提示。

Any input would be appreciated. 任何输入将不胜感激。 Thanks. 谢谢。

It seems that the match is on drive, type and date, so for new records, perhaps: 似乎匹配是在驱动器,类型和日期上,因此对于新记录,也许是:

INSERT INTO items_moved (drive, type, file_date, file_count, file_size) 
SELECT t.drive, type, t.file_date, t.file_count, t.file_size 
FROM temp_table t
LEFT JOIN items_moved m
ON t.Drive = m.Drive AND t.Type = m.Type and t.file_date = m.file_date
WHERE m.ID Is Null

A second query will be needed for updates: 需要第二个查询以进行更新:

UPDATE items_moved  m
INNER JOIN temp_table t
ON t.Drive = m.Drive AND t.Type = m.Type and t.file_date = m.file_date
SET m.file_count = t.file_count, m.file_size = t.file_size
WHERE m.file_size < t.file_size

Or there abouts. 或关于。 Is this what you mean? 你是这个意思吗?

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

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