473,407 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

UPDATE query with two tables problem

Warehouse shipping application. In the master table, I have Purchase_Order, Planned_Ship_Date, and Actual_Ship_Date. I get a daily update that I load as a staging table which contains updated Planned_Ship_Date and Actual_Ship_Date. Purchase_Order doesn't change, so it becomes the index.I have tried multiple queries to update the master table and can't get it to pick up the revised dates. Query Design yields the following:
Expand|Select|Wrap|Line Numbers
  1. UPDATE staging_table
  2.        INNER JOIN
  3.        master_table
  4.     ON (staging_table.[Actual_Ship_Date] = master_table.[Actual_Ship_Date])
  5.    AND (staging_table.[Planned_Ship_Date] = master_table.[Planned_Ship_Date])
  6. SET    staging_table.Planned_Ship_Date = [master_table].[Planned_Ship_date]
  7.      , staging_table.Actual_Ship_Date = [master_table].[Actual_Ship_Date];
The query above doesn't update anything. Do I need a WHERE that ties the UPDATE to Purchase_Order? Any and all help greatly appreciated!
Nov 20 '19 #1

✓ answered by NeoPa

Hi Kerry.

It's hard to know what you expect from this query and what you're actually trying to achieve. Currently, what it is doing is changing the two fields so that the values match between the two tables, but only if they already match.

I suspect it's actually working perfectly - just not doing what you expect it to. Therein lies the problem. Whatever it is you're expecting is certainly not what you've told it to do.
KerryOkie27:
Do I need a WHERE that ties the UPDATE to Purchase_Order?
Sort of. Actually, you need to rip out your current UPDATE clause and change how the two tables are linked. It's hard to say exactly how they should be linked as you don't say if the [staging_table] contains one or multiple items that match on [Purchase_Order] alone. The following should work if there can only ever be one record in the [staging_table]. If all of them are left in there from one day to the next then it gets more complicated (We'll deal with that if & when we have to) :
Expand|Select|Wrap|Line Numbers
  1. UPDATE [master_table] AS [MT]
  2.        INNER JOIN
  3.        [staging_table] AS [ST]
  4.     ON [MT].[Purchase_Order]=[ST].[Purchase_Order]
  5. SET    [MT].[Planned_Ship_Date]=[ST].[Planned_Ship_Date]
  6.      , [MT].[Actual_Ship_Date]=[ST].[Actual_Ship_Date]

6 1473
twinnyfo
3,653 Expert Mod 2GB
kerryokie27,

Welcome to Bytes!

Your code is telling the query to update the staging table.

I think you want it the other way around?

Hope that hepps!
Nov 20 '19 #2
NeoPa
32,556 Expert Mod 16PB
Hi Kerry.

It's hard to know what you expect from this query and what you're actually trying to achieve. Currently, what it is doing is changing the two fields so that the values match between the two tables, but only if they already match.

I suspect it's actually working perfectly - just not doing what you expect it to. Therein lies the problem. Whatever it is you're expecting is certainly not what you've told it to do.
KerryOkie27:
Do I need a WHERE that ties the UPDATE to Purchase_Order?
Sort of. Actually, you need to rip out your current UPDATE clause and change how the two tables are linked. It's hard to say exactly how they should be linked as you don't say if the [staging_table] contains one or multiple items that match on [Purchase_Order] alone. The following should work if there can only ever be one record in the [staging_table]. If all of them are left in there from one day to the next then it gets more complicated (We'll deal with that if & when we have to) :
Expand|Select|Wrap|Line Numbers
  1. UPDATE [master_table] AS [MT]
  2.        INNER JOIN
  3.        [staging_table] AS [ST]
  4.     ON [MT].[Purchase_Order]=[ST].[Purchase_Order]
  5. SET    [MT].[Planned_Ship_Date]=[ST].[Planned_Ship_Date]
  6.      , [MT].[Actual_Ship_Date]=[ST].[Actual_Ship_Date]
Nov 20 '19 #3
Thank you for your reply, NeoPa.

What I need to do is update the master table from the staging table anytime either (or both) of the dates change. The warehouse keeps the dates on an Excel spreadsheet, so I import the Excel sheet to create the staging table. There are typically 25 - 30 purchase orders that go into the staging table each day.

I'll enter your code above and see where it takes me - thanks again!
Nov 20 '19 #4
NeoPa
32,556 Expert Mod 16PB
Hi Kerry.

You could include a WHERE clause to restrict the updates to only those records where one or other of the dates have changed, but I guess, from your explanation, that the record would only exist in the staging table if one of them were different anyway.

One thing I'm still not clear about - Does the spreadsheet, and thus [staging_table], have the full history of updates or will it only contain those for today? It's important because if historical records are in there too then your SQL would need to be updated to handle that. It would run without the update but the results would be unpredictable.
Nov 20 '19 #5
Hi NeoPa,

What I'm currently doing is using Spreadsheet Compare (comes bundled with Office) and taking the results to build my staging file - so, currently, the only records in the staging file are those known to have changes.

That said, I'd like to eliminate this manual step, so I'd like to see the solution that allows older records to remain in the staging file. Thank you for all your help so far!
Nov 21 '19 #6
NeoPa
32,556 Expert Mod 16PB
Hi Kerry.

As things stand, from the information we currently have, we have no way of identifying which records of [staging_table] are relevant. Hopefully there is more information in this table, such as a date or maybe even a date/time value, that can allow us to select one of the records to use. Consider the hypothetical situation where a purchase order has three entries added over a period with three different sets of dates. How would the process recognise which of those three to use?

So far, all we know is that they have different values in [Planned_Ship_Date] & [Actual_Ship_Date] (from each other). We know of nothing (yet) to indicate which of the three is the one we would want to update [master_table] to.

Possibly there is an extra field we could use to identify which record is most appropriate. If so then you'll need to tell us about it. If not then how do you tell manually. Is it a simple case of ignoring records that weren't added most recently? If so then we need some data that reflects that in a way we can utilise. That's possible, but would involve storing a copy of the table as was and processing the compare internally in Access.

In short, we can't advise without all the relevant information from you.
Nov 21 '19 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: mo | last post by:
I need to bring the ssn's into UniqueSups (supervisors) from tblNonNormalized. My inherited DB is not normalized and I find it extremely irritating due to the workarounds needed. I created...
4
by: rdraider | last post by:
I am looking for some assistance with an update query that needs to link 3 tables: This query ran and reported over 230,000 records affected but did not change the field I wanted changed, not...
10
by: Hank1234 | last post by:
Can I use one Data Adapter and one Command Builder to update amny tables? Currently in my data adapter I query two tables and fill them into two tables in a data set. When I make a change to a...
2
by: JMCN | last post by:
hi i need some advice on whether if it would be better to use an append query or an update query. here is the situation, i have linked another database table to my current database. then i...
0
by: Dalan | last post by:
I'm not certain if this is doable in the manner explained below, but I'm certain that there have been Access developers who have done something similar. So whatever insight and assistance that can...
3
by: Megan | last post by:
hi everybody- it seems like my update query should work. the sql view seems logical. but that might be up to discussion...lol...i'm a newbie! UPDATE , Issue SET .IssueID = . WHERE ((.=.));
2
by: NigelMThomas | last post by:
I have an especially challenging problem. I know there are a few geniuses in this group so perhaps; you can advise me whether or not this can be done as an update query in Access. Thanks. I am...
1
by: G Gerard | last post by:
Hello I am trying to update a table (TableB) using records from a second table (TableA)
3
by: turtle | last post by:
I have Two tables (Table1 and Table2). Both tables have a common field called part number. Table 1 contains an extra field that i would like to update table 2 to match if the part number matches....
7
Brilstern
by: Brilstern | last post by:
I have a database that consist of two primary tables: I have created a button that deletes a patron from the tables INNERJOIN by opening a form requesting the user input the room to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.