Solved

joining one table to multiple fields?

Posted on 2013-10-30
4
174 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
4 Comments
 
LVL 24

Expert Comment

by:chaau
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now