简体   繁体   中英

SQL MS Access Conditional Update Query Referencing Other Tables

I am getting my feet wet in SQL, particularly because of the problem that I am working on now, I know how I would accomplish the task using a referential Countif formula in Excel but because I am dealing with close to 1 million records, Excel keeps bombing out on me and SQL seemed like a more efficient means to accomplish the task.

Background:

Basically I have a 'summary' table like this:

 ID_summary(ID_num, exist_2009, exist_2010, exist_2011, exist_2012)

I then have 4 tables each corresponding to 2009, 2010, 2011 and 2012 with just two columns in each table ID_num and date.

2009(ID_num,date)

The ID_summary table has a comprehensive list of all the unique IDs that exists in the four year tables. I created this list through unions of the four year table.

The problem:

I want to write an update query(?) to populate the ID_summary table with "TRUE" or a "1" for each "exists_20XX" column, for each record, if the ID number appears in the corresponding year tables.

For example, if ID 002 only appears in 2009 and 2012 then the row for ID 002 in the summary table should look like this

ID_summary(ID_num, exist_2009, exist_2010, exist_2011, exist_2012)
ID_002,TRUE,FALSE,FALSE,TRUE

I've done similar basic queries to do some data cleaning and have gotten successful results but it was always just referencing other fields in the same table to determine if a field should get re coded.

I am using MS Access 2010.

Any help is greatly appreciated!

OK, I checked here.

http://msdn.microsoft.com/en-us/library/office/bb221186%28v=office.12%29.aspx

Seems the syntax might be slightly different
for MS Access than it is for SQL Server.

Try these two in this order:

update ID_summary
set exist_2009 = 0 -- false

update
ID_summary ids
left join table_2009 t on ids.ID_num = t.ID_num
set
ids.exist_2009 = 1 -- true
where
(t.ID_Num is not null)

If it was SQL Server, one would need to
use a slightly different syntax.
But we're talking MS Access here.

update ids
set
ids.exist_2009 = 0 -- false
from
ID_summary ids

update ids
set
ids.exist_2009 = 1 -- true
from
ID_summary ids
left join table_2009 t 
on ids.ID_num = t.ID_num
where
(t.ID_Num is not null)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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