Solved

joining one table to multiple fields?

Posted on 2013-10-30
4
182 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

914 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

24 Experts available now in Live!

Get 1:1 Help Now