473,416 Members | 1,726 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,416 software developers and data experts.

Access2k I'm trying to fill a table from 3 source tables on condition...

Hi,

I have 3 tables of data created from different sources, each with the same 2 fields & I wanted to fill the 2nd field of another table with the data on condition.

The condition is because some sources have a higher known accuracy than other sources.

So, every table has a field named "SupplierCode" & a field named "SupplierName", There are 3 source tables (SourceTable1, SourceTable2, SourceTable3), & a final output table (ResultTable) which contains the desired SupplierCode's.

Where there's a match with ResultTable.SupplierCode & each of the SourceTable?.SupplierCode's, I want the ResultTable.SupplierName to be populated with SourceTable3.SupplierName data, then to be populated with SourceTable2.SupplierName data, then with SourceTable3.SupplierName data. But only if there's data.

So what I'm trying to say is SourceTable1.SupplierName data is most desired & SourceTable3.SupplierName data is least desired, but better than nothing. But I don't want a blank SourceTable1.SupplierName record to overwrite populated SourceTable3.SupplierName data. Sort of "only overwrite it if you've got data" condition.

I guess another way is to populate ResultTable.SupplierName with SourceTable1.SupplierName, then only populate SourceTable2.SupplierName into ResultTable.SupplierName if that record is still empty, & so on...

I hope this is making sense, sorry if it's vague. I have searched previous help here but I can't seem to make it fit.

My SQL statement so far is:
SELECT ResultTable.SupplierCode, ResultTable.SupplierName, SourceTable1.SupplierName, SourceTable2.SupplierName, SourceTable3.SupplierName
FROM ((ResultTable LEFT JOIN SourceTable1 ON ResultTable.SupplierCode = SourceTable1.SupplierCode) LEFT JOIN SourceTable2 ON ResultTable.SupplierCode = SourceTable2.SupplierCode) LEFT JOIN SourceTable3 ON ResultTable.SupplierCode = SourceTable3.SupplierCode;


This doesn't put the data into 1 column though (it shows all 3 sources), & it won't apply the condition, of course. Thanks in advance.


Dave.
Sep 5 '07 #1
5 1627
Note, the ResultTable.SupplierCode is already populated with the desired codes, because that was the result of a SELECT DISTINCT query on another data table.

I thought this extra bit of info may be important. Thanks.


Dave.
Sep 5 '07 #2
MMcCarthy
14,534 Expert Mod 8TB
Try this ...

Expand|Select|Wrap|Line Numbers
  1. INSERT INTO ResultTable (SupplierCode, SupplierName)
  2. SELECT SourceTable1.SupplierCode,  
  3. IIf(Not IsNull(SourceTable1.SupplierName), SourceTable1.SupplierName, 
  4. IIf(Not IsNull(SourceTable2.SupplierName), SourceTable2.SupplierName, SourceTable3.SupplierName)
  5. FROM SourceTable1 INNER Join SourceTable2
  6. On SourceTable1.SupplierCode = SourceTable2.SupplierCode
  7. INNER JOIN SourceTable3
  8. ON SourceTable2.SupplierCode = SourceTable3.SupplierCode
  9.  
This will populate ResultTable with new records corresponding to your requirements as I understand them.
Sep 5 '07 #3
MMcCarthy
14,534 Expert Mod 8TB
However, if as you say the ResultTable is already populated with SupplierCode then you need to use an update query as follows ...

Expand|Select|Wrap|Line Numbers
  1. UPDATE ResultTable Set SupplierName=
  2. SELECT SourceTable1.SupplierCode,  
  3. IIf(Not IsNull(SourceTable1.SupplierName), SourceTable1.SupplierName, 
  4. IIf(Not IsNull(SourceTable2.SupplierName), SourceTable2.SupplierName, SourceTable3.SupplierName)
  5. FROM SourceTable1 INNER Join SourceTable2
  6. On SourceTable1.SupplierCode = SourceTable2.SupplierCode
  7. INNER JOIN SourceTable3
  8. ON SourceTable2.SupplierCode = SourceTable3.SupplierCode
  9. WHERE ResultTable.SupplierCode = SourceTable1.SupplierCode
  10.  
Sep 5 '07 #4
Thanks so much for your help mmccarthy, it is much appreciated.

Unfortunately the code for the update query I tried (your 2nd post) gives the error:
Syntax error in query expression 'SELECT SourceTable1.SupplierCode

I tried to help myself rectify this error by:
- including a bracket before the SELECT at the beginning of line 2
- including a 2nd bracket at the end of line 4
- including a bracket after the FROM near the beginning of line 5
- including a bracket at the end of line 6
- including a bracket at the end of line 8

The code now runs, & looks like:

Expand|Select|Wrap|Line Numbers
  1. UPDATE ResultTable SET SupplierName=
  2. (SELECT SourceTable1.SupplierCode,  
  3. IIf(NOT IsNull(SourceTable1.SupplierName), SourceTable1.SupplierName, 
  4. IIf(NOT IsNull(SourceTable2.SupplierName), SourceTable2.SupplierName, SourceTable3.SupplierName))
  5. FROM (SourceTable1 INNER JOIN SourceTable2
  6. ON SourceTable1.SupplierCode = SourceTable2.SupplierCode)
  7. INNER JOIN SourceTable3
  8. ON SourceTable2.SupplierCode = SourceTable3.SupplierCode)
  9. WHERE ResultTable.SupplierCode = SourceTable1.SupplierCode

But when it runs, an input box appears with the title "Input Parameter Value", with the description "SourceTable1.SupplierCode".

To be able to advise which part of the SQL code is making the input box appear, I did some testing & found it to be the very last text after the equals on line 9. No matter what value I enter into the input box it only tries to look for one record.

As I said, thanks for your help on this. Could it be that line 9 should have nested IIF() statements as well?

Also, I'd like the number of source tables to increase to 5, How does that look for the code? I'm guessing deeper nesting on the IIF()'s & more on the INNER JOIN's.

Also, for simpler SQL code I did in Access2k for this, it uses a LEFT JOIN. Should the JOIN be a LEFT JOIN, an INNER JOIN, or doesn't it matter?


Dave.
Sep 6 '07 #5
MMcCarthy
14,534 Expert Mod 8TB
Sorry this was my fault. Thats what I get for copying and pasting ...

Try this now

Expand|Select|Wrap|Line Numbers
  1. UPDATE ResultTable SET SupplierName=
  2. (SELECT IIf(NOT IsNull(SourceTable1.SupplierName), SourceTable1.SupplierName, 
  3. IIf(NOT IsNull(SourceTable2.SupplierName), SourceTable2.SupplierName, SourceTable3.SupplierName))
  4. FROM (SourceTable1 INNER JOIN SourceTable2
  5. ON SourceTable1.SupplierCode = SourceTable2.SupplierCode)
  6. INNER JOIN SourceTable3
  7. ON SourceTable2.SupplierCode = SourceTable3.SupplierCode)
  8. WHERE ResultTable.SupplierCode = SourceTable1.SupplierCode
  9.  
Also, I'd like the number of source tables to increase to 5, How does that look for the code? I'm guessing deeper nesting on the IIF()'s & more on the INNER JOIN's.
Basicly yes. If the above works try it yourself and let me know if you run into any problems.

LEFT JOIN would be appropriate if all tables don't contain the full supplier code list. However, it would only work in that case if one of the tables did. It's a different problem.

Have a look at this tutorial on SQL JOINS
Sep 6 '07 #6

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

Similar topics

10
by: Blake | last post by:
I have created an Access2K front end application that connects to a SQLServer2K backend. I use this vba code to create the connection from the Access app: Dim strConnect As String 'make sure...
4
by: dschl | last post by:
Hi, I'm converting an Access 2000 database to Sql Server and must be missing something obvious. Using the Import utility in Sql Server, the Access queries seem to get executed and the...
0
by: Lauren Quantrell | last post by:
I'm trying to drop a file from Windows Explorer (or desktop, etc.) onto a field in Access2K and capture the full file path. I found an posting below that says this is possible but I cannot...
4
by: Mike Dole | last post by:
I might have taken a wrong turn trying to do an update on a database the way I did but maybe somebody can help me out cause I'm stuck.. I have to update 12 tables in a database in a client...
4
by: lorirobn | last post by:
Hello, I'd be curious to hear other thoughts on my database and table design. My database is for 'space use' in a lodging facility - will hold all spaces, like rooms (lodging rooms, dining...
1
by: Henry | last post by:
I hope someone can help me on this. I have two controls on a form. the first is a comboBox the second is a datagrid. Both controls are bound to tables in a common dataset. What I am trying to...
4
by: Dave Edwards | last post by:
I understand that I can fill a datagrid with multiple queries, but I cannot figure out how to fill a dataset with the same query but run against multiple SQL servers, the query , table structure...
0
by: Marcin Podle¶ny | last post by:
Hello! I use DataGrid which displays data in several columns. Number of columns depends on user prefferences (I mean: this is still the same query filling datatable but I use...
3
by: Marc Llenas | last post by:
Hello all, I'm new to ASP.NET and I have a question regarding the fill method of a dataset. Here is my code 1 Dim objConn As OleDbConnection 2 Dim objAdapt As OleDbDataAdapter 3 Dim...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.