QUERY to return data from primary table by checking the criteria in mapping/2nd table.

pedie

Well-known Member
Joined
Apr 28, 2010
Messages
3,875
Hi Everyone!

I'm trying to create a QUERY to return data from primary table by checking the criteria in mapping/2nd table.
I tried using
Code:
Code:
SELECT Table1.ID, Table1.users, Table1.mydate, Table1.ContactNo
FROM Table2 INNER JOIN Table1 ON (Table2.mydate = Table1.mydate) AND (Table2.users = Table1.users);


but it returns all matching records from both tables which I do not want.
What I want is to return data from table2 only if mydate & users matches in both the tables.
In the same time I want to get contact info of user from table1 which is not available in table2.
Excel Workbook
FGHIJKLM
1tb2table1
2IDusersmydateIDusersmydateContactNo
31user11-Jan-136user11-Jan-13123-123-45
42user21-Jan-131user11-Jan-13123-123-45
53user31-Jan-137user21-Jan-13123-123-47
64user41-Jan-132user21-Jan-13123-123-47
75user51-Jan-138user31-Jan-13123-123-52
86user51-Jan-133user31-Jan-13123-123-52
97user51-Jan-139user41-Jan-13123-123-55
108user61-Jan-134user41-Jan-13123-123-55
1110user51-Jan-13123-123-59
125user51-Jan-13123-123-59
1311user61-Jan-13123-123-90
Sheet1
Table Last Updated 5/7/2013 11:53:25 PM
Excel Workbook
OPQR
1Expected result
2IDusersmydateContactNo
31user11-Jan-13123-123-45
42user21-Jan-13123-123-47
53user31-Jan-13123-123-52
64user41-Jan-13123-123-55
75user51-Jan-13123-123-59
86user51-Jan-13123-123-59
97user51-Jan-13123-123-59
108user61-Jan-13123-123-90
Sheet1
Table Last Updated 5/7/2013 11:53:50 PM
Excel Workbook
ABCD
1Query result
2IDusersmydateContactNo
36user11-Jan-13123-123-45
41user11-Jan-13123-123-45
57user21-Jan-13123-123-47
62user21-Jan-13123-123-47
78user31-Jan-13123-123-52
83user31-Jan-13123-123-52
99user41-Jan-13123-123-55
104user41-Jan-13123-123-55
1110user51-Jan-13123-123-59
125user51-Jan-13123-123-59
1310user51-Jan-13123-123-59
145user51-Jan-13123-123-59
1510user51-Jan-13123-123-59
165user51-Jan-13123-123-59
1711user61-Jan-13123-123-90
Sheet1
Table Last Updated 5/7/2013 11:54:01 PM
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
This isn't clear. What are the ID's for? Why are the records repeating users/dates in table2 and users/dates/contacts in table1? What information do you need from this query?
ξ
 
Upvote 0
Hi Xen, thanks for looking into this. The ID field is like a sort of serial #, please ignore it.
Both tables can have same user multiple times. I want return table2 user record information if user exist in table1. and i want the contact information to come from table2.
 
Upvote 0
Do the dates matter? If not this might work (try and see):
Code:
SELECT Table2.ID, Table2.users, Table2.mydate, Table2.ContactNo
FROM Table2 WHERE Table2.Users IN (SELECT DISTINCT Table1.users FROM Table1);

If the dates do matter, then maybe:
Code:
SELECT Table2.ID, Table2.users, Table2.mydate, Table2.ContactNo
FROM Table2 
    INNER JOIN 
         (
         SELECT DISTINCT Table1.users, Table1.myDate 
         FROM Table1
         )
         AS TSub
ON Table2.users = TSub.users AND Table2.mydate = TSUB.myDate
;
 
Upvote 0

Forum statistics

Threads
1,214,859
Messages
6,121,963
Members
449,059
Latest member
oculus

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top