Left join

jerkyjerk

New Member
Joined
Sep 7, 2009
Messages
34
Is it okay to use direct values in ON statement after the LEFT JOIN?

Code:
SELECT A.*, B.*,C.* 
 FROM ((A 
  LEFT JOIN B ON B.F1 = A.F1 AND B.F2 = A.F2) 
  LEFT JOIN C  ON [COLOR="#FF0000"]C.F3 = '201504' [/COLOR]AND C.F1 = A.F1 AND C.F2 = A.F2) 
    WHERE A.F1='0051004' AND A.F2='USD' AND A.F3='201505';

Problem here is on C.F3 = '201504' , insted of C.F3 = A.F3 I used C.F3 = '201504' .
'201504' will be generated from a function in my excel vba code.

Is there a way where I can use the function return value instead of the field name.
Or how can i correct this SELECT statement?
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
it might work in an on clause (it would in SQL Server - not sure about Access).

But as a rule of thumb in any database engine, that goes in the WHERE clause:
Code:
SELECT A.*, B.*,C.* 
 FROM ((A 
  LEFT JOIN B ON B.F1 = A.F1 AND B.F2 = A.F2) 
  LEFT JOIN C  ON C.F1 = A.F1 AND C.F2 = A.F2) 
    WHERE 
        A.F1='0051004' 
        AND A.F2='USD' 
        AND A.F3='201505'[COLOR="#FF0000"]
        AND C.F3 = '201504'[/COLOR];

Or if you have a function:
Code:
SELECT A.*, B.*,C.* 
 FROM ((A 
  LEFT JOIN B ON B.F1 = A.F1 AND B.F2 = A.F2) 
  LEFT JOIN C  ON C.F1 = A.F1 AND C.F2 = A.F2) 
    WHERE 
        A.F1='0051004' 
        AND A.F2='USD' 
        AND A.F3='201505'[COLOR="#FF0000"]
        AND C.F3 = MyFunction()[/COLOR];
 
Upvote 0
it might work in an on clause (it would in SQL Server - not sure about Access).

But as a rule of thumb in any database engine, that goes in the WHERE clause:
Code:
SELECT A.*, B.*,C.* 
 FROM ((A 
  LEFT JOIN B ON B.F1 = A.F1 AND B.F2 = A.F2) 
  LEFT JOIN C  ON C.F1 = A.F1 AND C.F2 = A.F2) 
    WHERE 
        A.F1='0051004' 
        AND A.F2='USD' 
        AND A.F3='201505'[COLOR=#ff0000]
        AND C.F3 = '201504'[/COLOR];

Or if you have a function:
Code:
SELECT A.*, B.*,C.* 
 FROM ((A 
  LEFT JOIN B ON B.F1 = A.F1 AND B.F2 = A.F2) 
  LEFT JOIN C  ON C.F1 = A.F1 AND C.F2 = A.F2) 
    WHERE 
        A.F1='0051004' 
        AND A.F2='USD' 
        AND A.F3='201505'[COLOR=#ff0000]
        AND C.F3 = MyFunction()[/COLOR];
Thanks!

I tried to put it in the WHERE clause.
It works now..

I thought that conditions for the WHERE clause are only for the Original table, in this case Table A.
But I was wrong.. :)
 
Upvote 0

Forum statistics

Threads
1,221,848
Messages
6,162,415
Members
451,762
Latest member
Brainsanquine

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