sql - MS Access 2007 update/insert or merge or union -
i have been searching quite while how should pretty easy in ms access 2007.
i have main table, items_moved, tracks number of files received, type, day. basic structure is:
id (autonumber), drive (text), type (text), date (datetime), file_count (number), file_size (number)
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 temp table exact same data structure. temp table generated starting (and including) last day of items_moved table , finding what's new.
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 append temp table on main table that:
- there no duplicates (based on drive , date)
- if temp table has matching drive , date , larger values overwrites main table
- if row exists in temp table not main table, gets appended end of main .
the result in items_moved table records 1-1776 unchanged, end this:
1777, f:\snaps, pics, 6/09/2010, 66, 151616131 (unchanged) 1778, f:\snaps, pics, 6/10/2010, 366, 6531616131 (updated temp 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 can think of.
i can rows matching dates, can use update statement. can't rows temp don't have matching dates in items_moved table, can't run insert.
the final point execute part of vb action when user requests report access db.
here's snippet
insert 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) temp_table temp_table.drive = 'f:\snaps' group temp_table.created
this works, adds end of table. i'm left problem of duplicates. i'm sure there's way de-duplicate table on fly, seems waste. i've found hints on using union , nothing on how use union update 1 of tables in union.
any input appreciated. thanks.
it seems match on drive, type , date, new records, perhaps:
insert items_moved (drive, type, file_date, file_count, file_size) select t.drive, type, t.file_date, t.file_count, t.file_size temp_table t left join items_moved m on t.drive = m.drive , t.type = m.type , t.file_date = m.file_date m.id null
a second query needed updates:
update items_moved m inner join temp_table t on t.drive = m.drive , t.type = m.type , t.file_date = m.file_date set m.file_count = t.file_count, m.file_size = t.file_size m.file_size < t.file_size
or there abouts. mean?
Comments
Post a Comment