Sql recordset

jerkyjerk

New Member
Joined
Sep 7, 2009
Messages
34
Its an SQL question.
Im trying to embed SQL statement on my VBA.
I have the following tables

Table1 with Fields (Field1, Field2, Field3, Field4)
and
Table2 with Fields (Field1, Field2, Field5, Field6)
Their common fields are Field1 and Field2

I want to join these two tables based from the WHERE conditions of Table1
But whenever I tried to use INNER JOIN, they get different record counts.

This is the correct record counts
RECORD COUNT is 68:
Code:
SELECT COUNT(*) FROM TABLE1 A
 WHERE A.Field1='2015/07/15' And A.Field2>'9999999'

When I try to use INNER JOIN
RECORD COUNT is only 4
Note: the record count 4 are the 4 records on TABLE2

Code:
SELECT COUNT(*) FROM TABLE1 A
[COLOR=#ff0000]  INNER JOIN TABLE2 B
  ON A.Field1=B.Field1 AND A.Field2=B.Field2[/COLOR]
 WHERE A.Field1='2015/07/15' And A.Field2>'9999999'

What seems to be the problem.
I need to get the 68 records from TABLE1 with the added fields from TABLE2
Please help..
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
The inner join will only return records where the values are in both tables with respect to the fields that have been joined - this is the way it's supposed to work. Without knowing more about the data and relationships between the table fields, we can only guess. Try an outer join (I presume left outer join). If neither left or right outer joins work, you will probably have to create a UNION query.
 
Upvote 0
Glad to help! There are more query "tricks" you may have to learn to get what you want in the future. Like 'unique records' and 'unique values' for instance.
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,122
Members
452,381
Latest member
Nova88

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