Question : Access 2007 SQL statement needed to merge two tables

I have a table called 'STORES' and a table called 'PRICES'

'STORES' table:
each row in this table represents one unique STORE address.
there is only one row per store location.
this table ALSO contains ALL of the prices for ALL of the items that store carries.
(I realize this is not the most efficient way to store this data, but this is how the data comes to me from the client)

StoreID   Addr1   City   State   Zip   Item1   Item2   Item3   Item4
25   123 Main St  Ft Laud  FL   33312  $2.99  $1.99  $3.99  $8.99
27   8700 1st St  Miami     FL   33150  $2.49  $1.89  $4.09  $9.19
36   22 NW 8 St   Tampa   FL   34765  $3.29  $1.85  $3.89  $8.49


'PRICES' table:
each row represents a specific price change for a specific item at a specific store:
StoreID   ItemCode   NewPrice
27           Item2         $1.89
27           Item3         $3.99
36           Item1         $3.19
36           Item2         $1.95
36           Item4         $8.19


The 'STORE' list is a master list that contains all of the stores, and all of the prices for all of the items in every store

Store locations can update the price of any item in their store at any time.
When this happens, a 'PRICE' data file is generated (like the sample above)
Rather than containing all of the pricing for all of the items in all of the stores, it ONLY contains price changes

So in the example above you can see that, if a store changes the price for 3 items, the PRICE data file will contain 3 rows.
If the store changes the price for 17 items, then there will be 17 rows having that store number.

So once per week I need to take all of the data in the 'PRICE' data file, and use it to update the master list ('STORES')

The SQL query first needs to search STORES, find store #27, and update the price for Item2
Then it needs to find store #27 and update the price for Item3
and so on and so one, once for each row (record) in the PRICE data file.

I planned to do this with Access 2007.
I was able to import both tables (STORES and PRICES) but now what? I'm lost...

Answer : Access 2007 SQL statement needed to merge two tables

There are several solutions for your problem. May be the most suitable is using VBA code. But I will try to find a solution using only SQL statements.

First, create a cross reference query called qryChanges from the prices table using the storeID as row grouping and Item Code as column group. Select the column names in the query parameters form to include all the available item codes, something like "Item1","Item2","Item3" ... The value is the first price.


The result of this query will be like:

StoreID     Item1     Item2    item3   item 4
27                                1.89     3.99    
28              3.19         1.41                      8.19

Then create another query, as an update query to update table stores. This one uses the table Stores and the previous query qryChanges. Relate them by using the StoreId field. Then for each item, update it by using:
for item1
iif(isnull(qryChanges.Item1),StoreId.Item1,qryChanges.Item1)
for item2
iif(isnull(qryChanges.Item2),StoreId.Item2,qryChanges.Item2)

Summarizing, If the qryChanges contains a null, get the previous value, if not use the new.

Regards,
Jose
Random Solutions  
 
programming4us programming4us