INNER JOIN SQL on VBA

jerkyjerk

New Member
Joined
Sep 7, 2009
Messages
34
Anyone who could help me with my VBA problem regarding SQL Query(JOIN)

I have three tables:
TableA with fields(F1, F2, F3, F4, F5)
TableB with fields(F0, F1, F6, F7, F8)
TableC with fields(F0,F1,F6)

Now, with TableB.F0='2010', I want to get the
records from Table A and Table B to be placed in TableC where (TableA.F1=TableB.F1 AND TableB.F6='1')
OR if no records found from TableB,
It will just get all the records as new records to be place to Table C from TableB where TableB.F6='1'

I have been trying some INNER JOIN, LEFT JOIN not successul on this one. as much as possible I would like to make this QUERT in one SELECT statement.

Can somebody help me?
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Hi
If I uderstand you right
You need TableB records that are not in TableA
Code:
Select TableT.F0,TableT.F1,TableT.F6
From (Select TableB.F0,TableB.F1,TableB.F6 From TableB Where TableB.F6='1') As TableT
Left Join TableA On (TableA.F1=TableT.F1)
Where (TableA.F1 Is Null)
Regards,
 
Upvote 0
Hi
If I uderstand you right
You need TableB records that are not in TableA
Code:
Select TableT.F0,TableT.F1,TableT.F6
From (Select TableB.F0,TableB.F1,TableB.F6 From TableB Where TableB.F6='1') As TableT
Left Join TableA On (TableA.F1=TableT.F1)
Where (TableA.F1 Is Null)
Regards,

Hello anvg! :-)
Thanks for your reply.

From these Two tables:
TableA with fields(F1, F2, F3, F4, F5)
TableB with fields(F0, F1, F6, F7, F8)

Step1: I want to get first all F1 values from TableA where F3='2010'
Step2: Then will select records from both tableA and Table B where extracted TableA.F1 from Step 1 is equal to TableB.F1 AND TableB.F6='1'
Step3: If TableA.F1 <> TableB.F1, it will still select records from TableB WHERE TableB.F6 ='1'

As you suggested;

I did this SELECT statement
Code:
Select TableC.F0, TableC.F1, TableC.F6, TableC.F7, TableC.F8
 FROM (TableB.F0, TableB.F1, TableB.F6, TableB.F7, TableB.F8 FROM TableB WHERE TableB.F6='1') as Table C
 LEFT JOIN TableA ON (TableA.F1=TableC.F1)

I wonder where should I put the F3='2010' so that I can get all the F1 values from TableA
to complete STEP1.
 
Upvote 0

Forum statistics

Threads
1,222,615
Messages
6,167,065
Members
452,093
Latest member
JamesFromAustin

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