?
Solved

SQL inner join question

Posted on 2007-10-11
5
Medium Priority
?
284 Views
Last Modified: 2010-03-20
Dear Gurus
I have two tables, DB_ALL_1 and DB_ALL_2, the first has 1,200 records, the second has 13,000 records.  What I want to do is run a query that lists those phone numbers that are common to both tables.

So, I wrote an SQL Inner Join query, but for some reason, it's returning 7,300 records.  Intuitively, the number of records that are common to both tables has to be less than the number of records in the smaller table (1,200), yes.  However, there are many duplicate phone numbers in the result.

So, I would most appreciate a correction to the code so that only the unique phone numbers are extracted.  I tried a 'DISTINCT' phrase after 'SELECT', but the result still returned 7,300.
        SQLStr = "SELECT DISTINCT * FROM DB_ALL_1" & _
            " INNER JOIN DB_ALL_2" & _
            " ON DB_ALL_1.ACC_PHONE = DB_ALL_2.ACC_PHONE" & _
            " WHERE DB_ALL_1.ACC_PHONE = DB_ALL_2.ACC_PHONE"

             Recordset1.Open SQLStr, DbsConnection, adOpenStatic
             Recordset1.MoveLast
             Recordset1.MoveFirst
             RecordCount = Recordset1.RecordCount

             While Not Recordset1.EOF
                         Phone = Recordset1("DB_ALL_1.ACC_PHONE")
                         Recordset1.MoveNext
             Wend
0
Comment
Question by:BrianBeck
5 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1000 total points
ID: 20056001
>Intuitively, the number of records that are common to both tables has to be less than the number of records in the smaller table (1,200), yes.
yes, but as you perform a inner join, and a single row in DB_ALL_1 might have several matches in DB_ALL_2, hence returning multiple rows...

to use DISTINCT, you need to avoid the *, as distinct will apply on the entire result set...
use DISTINCT DB_ALL_1.ACC_PHONE, and it will work better:

        SQLStr = "SELECT DISTINCT DB_ALL_1.ACC_PHONE FROM DB_ALL_1" & _
            " INNER JOIN DB_ALL_2" & _
            " ON DB_ALL_1.ACC_PHONE = DB_ALL_2.ACC_PHONE" & _
            " WHERE DB_ALL_1.ACC_PHONE = DB_ALL_2.ACC_PHONE"
0
 
LVL 23

Assisted Solution

by:Ashish Patel
Ashish Patel earned 1000 total points
ID: 20056002
Try
SQLStr = "SELECT DISTINCT DB_ALL_1.ACC_PHONE FROM DB_ALL_1" & _
            " INNER JOIN DB_ALL_2" & _
            " ON DB_ALL_1.ACC_PHONE = DB_ALL_2.ACC_PHONE" & _
            " WHERE DB_ALL_1.ACC_PHONE = DB_ALL_2.ACC_PHONE"
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 20056007
Hello cclambie,

if you put Distinct *, it will consider all the columns and will take the distict among those rows for eg, if the values in the able are

a , b , 2007-01-01 01:01:001
a , b , 2007-01-01 01:01:002
a , b , 2007-01-01 01:01:003

Now the distinct statement will return all the rows, as they differ data on the last column




Aneesh R
0
 
LVL 18

Expert Comment

by:UnifiedIS
ID: 20056053
Your join and where clause are redundant and both can be replaced with a subquery:
INNER JOIN (SELECT DISTINCT ACC_PHONE FROM DB_ALL_2) A2
ON A2.ACC_PHONE = DB_ALL_1.ACC_PHONE

Your intuition is off in this case, it would appear that there is a one-to-many (or many to many) relationship between the tables.  Your distinct call ensures that the each record of the result is unique but by including all the columns you get more results than 1200.  
0
 

Author Comment

by:BrianBeck
ID: 20056059
Thank you all.  I've increased the points to 500 and split between the solution providers, angelIII: and asvforce.
Thanks!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

864 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