Solved

joining one table to multiple fields?

Posted on 2013-10-30
4
185 Views
Last Modified: 2014-01-08
I have one table that I need to join to a second table but am having trouble getting the results I want..


ltblGSA_Orders_IOrders                               ltblQBooks_Customer
cust_name                                                     name
email                                                              email (this field is a memo hence the sql that follows)

I have a left join from ltblgsa_orders_IOrders.email to ltblqbooks_Customer.email (should I add another join from cust_name to name?)

an example of the data contained is:

ltblGSA_Orders_IOrders                               ltblQBooks_Customer
JOHN SMITH                                                  Smith, John
john.smith@abc.com                                   jsmith@def.com

here's the expression I've been using but it hasn't been working:

IIf(
      [ltblGSA_Orders_IOrders].[cust_email]=cstr([ltblQBooks_Customer].[email] & ""),
            [ltblQBooks_Customer]![Name],
IIf(
      StrConv(Nz(Mid([CUST_NAME],InStr([CUST_NAME]," ")+1)),3) & ", " & StrConv(Nz(Left([CUST_NAME],InStr([CUST_NAME]," ")-1)),3)=[ltblQBooks_Customer]![Name]  ,
            "DuplicateNameDifferentCustomer" & StrConv(Nz(Mid([CUST_NAME],InStr([CUST_NAME]," ")+1)),3) & ", " & StrConv(Nz(Left([CUST_NAME],InStr([CUST_NAME]," ")-1)),3),
                        StrConv(Nz(Mid([CUST_NAME],InStr([CUST_NAME]," ")+1)),3) & ", " & StrConv(Nz(Left([CUST_NAME],InStr([CUST_NAME]," ")-1)),3)
))

hopefully the above isn't confusing.

thanks for your help!
0
Comment
Question by:intsup
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 25

Expert Comment

by:chaau
ID: 39613079
What are these Iif statements? Are these for the Column expression or they are the conditions you use for the JOIN?
0
 
LVL 8

Expert Comment

by:virtuadept
ID: 39624432
It looks like you want to join 2 tables but those 2 tables do not have a common identifier to join against. In other words, you have 2 tables that have a "name" column but they do not store the name consistantly and not in the same way. For this I say, good luck with that. What you need to do, really, is create some new columns on both tables and write some update statements so that you will get these new columns in the same format on both tables so that you can use those columns to join against. And put an index on them for these new columns also.
0
 
LVL 5

Accepted Solution

by:
Lawrence Barnes earned 500 total points
ID: 39670652
I understand that sometimes databases are inherited and you have to work with what you have.  In the long run some structure (noted by virtuadept) would help.  But in the short term:

Emails are fairly unique and you are left joining on them so you will see all orders with matching emails.  But I suppose email addresses can be recycled so you want to consider comparing names, since they are in both of the tables...just in case.  In the attached I've created goofy@aol.com as an email address that switched owners.

Since you are using an IIF I'm assuming you are doing this in Access which is what I have attached.

To this end...Regarding your code:  In the table references above you have the field ltblGSA_Orders_IOrders.email.  But in your query you refer to ltblGSA_Orders_IOrders.cust_email and that is why that statement didn't work.  Here's one that does (and is in the attached database in Query1.YourQueryName.)
SELECT ltblGSA_Orders_IOrders.cust_name, ltblGSA_Orders_IOrders.email, ltblQBooks_Customer.name, ltblQBooks_Customer.email AS CustomerEmail, IIf([ltblGSA_Orders_IOrders].[email]=CStr([ltblQBooks_Customer].[email] & ""),[ltblQBooks_Customer]![Name],IIf(StrConv(Nz(Mid([CUST_NAME],InStr([CUST_NAME]," ")+1)),3) & ", " & StrConv(Nz(Left([CUST_NAME],InStr([CUST_NAME]," ")-1)),3)=[ltblQBooks_Customer]![Name],"DuplicateNameDifferentCustomer" & StrConv(Nz(Mid([CUST_NAME],InStr([CUST_NAME]," ")+1)),3) & ", " & StrConv(Nz(Left([CUST_NAME],InStr([CUST_NAME]," ")-1)),3),StrConv(Nz(Mid([CUST_NAME],InStr([CUST_NAME]," ")+1)),3) & ", " & StrConv(Nz(Left([CUST_NAME],InStr([CUST_NAME]," ")-1)),3))) AS AltName
FROM ltblGSA_Orders_IOrders LEFT JOIN ltblQBooks_Customer ON ltblGSA_Orders_IOrders.email = ltblQBooks_Customer.email;

Open in new window

Database3.accdb
0
 

Author Comment

by:intsup
ID: 39765167
i'm afraid I got caught up with two other projects and wasn't able to work on this one at all so i'll close for now until I get back to this one..

thanks for the input, though!
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question