Using subquery in a join statement in ADO SQL Excel VBA

pcanino

New Member
Joined
Nov 6, 2015
Messages
11
Hi everyone - if anyone can figure out what is wrong with my sqlstatement here because I'm really out of ideas on how to resolve this, i have scoured the net for days already.

Here's the query:
SELECT Sup.[SAP ID] FROM [Supply$] AS LEFT JOIN [Demand$] AS [Dem] ON (Sup.[Career Level] IN (SELECT [Range Level 1] FROM [Matrix$] AS [M] WHERE M.[Range Level] = Dem.[Career Level]))

I'm executing this in Excel and I get this error:
"[Microsoft][ODBC Excel Driver] Syntax error in query expression 'Sup.[Career Level] IN (SELECT [Range Level 1] FROM [Matrix$] AS [M] WHERE M.[Range Level] = Dem.[Career Level]'."
 
No the sql you gave me did not work.

That's if Sup.[gcp status] IS NULL then I convert it to blank because we treat NULL values as blank, that's why I put that if statement. I'm really out of ideas, anyway, we can just focus on the first sql statement I gave, that was really where my problem is.
 
Upvote 0

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Please post the code you are using to test my first sql query :)

P.S use code tags as well - it makes things much easier to read

Try it with the nast parentheses in the correct place
Rich (BB code):
SELECT Sup.[sap id] 
FROM   (([supply$] AS  
       LEFT JOIN [demand$] AS [Dem] 
              ON sup.[career level] = Dem.[career level] )
       LEFT JOIN [matrix$] AS [M] 
              ON M.[range level] = dem.[career level] )
WHERE  ( dem.[career level] IS NULL 
          OR M.[range_level] IS NOT NULL )
 
Last edited:
Upvote 0
I tried your code, it did work but not exactly the result I've expected. I'm really out of idea here already. I think the subquery in the join clause does not really work after all.
 
Upvote 0
I don't really understand why you don't just put all your criteria in the where clause and forget about the correlated query
 
Upvote 0
I see, sorry for the confusion, I interchanged the tables. The query should be like this:
Code:
SELECT Dem.*             FROM ([Demand$] AS [Dem] 
            LEFT JOIN [Supply$] AS [Sup]
              ON Sup.[Career Level] IN (SELECT [Range Level 1] FROM [Matrix$] AS [M] WHERE M.[Range Level] = Dem.[Career Level]))

The purpose of this query is to get all the demand values without any criteria then get all the supply values that will match the criteria only. So the possible values would be that there maybe demand values without any supply values. If I put the criteria in the "where clause" then it will already give me a different output.
 
Last edited:
Upvote 0
Not if you check if Sup is null or has a value in each criteria. That will give you both options then.
 
Upvote 0
I *think* you want something like:
<font face="Courier New" size="2">
<font color = "blue">SELECT</font> <font color = "maroon">[sap id]</font><font color = "silver">,</font>
<br/>       <font color = "maroon">[personnel name]</font><font color = "silver">,</font>
<br/>       <font color = "maroon">Sup</font><font color = "silver">.</font><font color = "maroon">[career level]</font><font color = "silver">,</font>
<br/>       <font color = "maroon">[capability]</font><font color = "silver">,</font>
<br/>       <font color = "maroon">[skill]</font><font color = "silver">,</font>
<br/>       <font color = "maroon">[final proficiency]</font><font color = "silver">,</font>
<br/>       <font color = "maroon">[skill type]</font><font color = "silver">,</font>
<br/>       <font color = "maroon">[current ig]</font><font color = "silver">,</font>
<br/>       <font color = "maroon">[pm]</font><font color = "silver">,</font>
<br/>       <font color = "#FF0080"><b>Iif</b></font><font color = "maroon">(</font><font color = "maroon">[sap id]</font> <font color = "blue">IS</font> <font color = "blue">NOT</font> <font color = "blue">NULL</font>
<br/>           <font color = "blue">AND</font> <font color = "maroon">[onsite country]</font> <font color = "silver">=</font> <font color = "red">'USA'</font><font color = "silver">,</font> <font color = "#FF0080"><b>Datediff</b></font><font color = "maroon">(</font><font color = "red">'m'</font><font color = "silver">,</font> <font color = "maroon">[last repatriation date]</font><font color = "silver">,</font> <font color = "#FF0080"><b>Now</b></font><font color = "maroon">(</font><font color = "maroon">)</font><font color = "maroon">)</font><font color = "silver">,</font> <font color = "red">''</font><font color = "maroon">)</font>                                                                                            <font color = "blue">AS</font> <font color = "maroon">[Last Repatriation]</font><font color = "silver">,</font>
<br/>       <font color = "#FF0080"><b>Iif</b></font><font color = "maroon">(</font><font color = "maroon">[sap id]</font> <font color = "blue">IS</font> <font color = "blue">NOT</font> <font color = "blue">NULL</font>
<br/>           <font color = "blue">AND</font> <font color = "maroon">[onsite country]</font> <font color = "silver">=</font> <font color = "red">'USA'</font><font color = "silver">,</font> <font color = "#FF0080"><b>Datediff</b></font><font color = "maroon">(</font><font color = "red">'m'</font><font color = "silver">,</font> <font color = "maroon">Sup</font><font color = "silver">.</font><font color = "maroon">[visa start date]</font><font color = "silver">,</font> <font color = "#FF0080"><b>Now</b></font><font color = "maroon">(</font><font color = "maroon">)</font><font color = "maroon">)</font><font color = "silver">,</font> <font color = "red">''</font><font color = "maroon">)</font>                                                                                               <font color = "blue">AS</font> <font color = "maroon">[Months Aging]</font><font color = "silver">,</font>
<br/>       <font color = "#FF0080"><b>Iif</b></font><font color = "maroon">(</font><font color = "maroon">[sap id]</font> <font color = "blue">IS</font> <font color = "blue">NOT</font> <font color = "blue">NULL</font><font color = "silver">,</font> <font color = "#FF0080"><b>Iif</b></font><font color = "maroon">(</font><font color = "maroon">[onsite country]</font> <font color = "silver">=</font> <font color = "red">'USA'</font><font color = "silver">,</font> <font color = "#FF0080"><b>Iif</b></font><font color = "maroon">(</font><font color = "maroon">[visa type]</font> <font color = "silver">=</font> <font color = "red">'H-1B'</font>
<br/>                                                                    <font color = "blue">OR</font> <font color = "maroon">(</font> <font color = "maroon">(</font> <font color = "maroon">[visa type]</font> <font color = "silver">=</font> <font color = "red">'L-1B'</font>
<br/>                                                                            <font color = "blue">OR</font> <font color = "maroon">[visa type]</font> <font color = "silver">=</font> <font color = "red">'L-1A'</font> <font color = "maroon">)</font>
<br/>                                                                         <font color = "blue">AND</font> <font color = "#FF0080"><b>Datediff</b></font><font color = "maroon">(</font><font color = "red">'m'</font><font color = "silver">,</font> <font color = "maroon">Sup</font><font color = "silver">.</font><font color = "maroon">[visa start date]</font><font color = "silver">,</font> <font color = "#FF0080"><b>Now</b></font><font color = "maroon">(</font><font color = "maroon">)</font><font color = "maroon">)</font> <font color = "silver">>=</font> <font color = "black">6</font>
<br/>                                                                         <font color = "blue">AND</font> <font color = "#FF0080"><b>Datediff</b></font><font color = "maroon">(</font><font color = "red">'m'</font><font color = "silver">,</font> <font color = "maroon">[last repatriation date]</font><font color = "silver">,</font> <font color = "#FF0080"><b>Now</b></font><font color = "maroon">(</font><font color = "maroon">)</font><font color = "maroon">)</font> <font color = "silver">>=</font> <font color = "black">6</font> <font color = "maroon">)</font><font color = "silver">,</font> <font color = "red">'Yes'</font><font color = "silver">,</font> <font color = "red">'No'</font><font color = "maroon">)</font><font color = "silver">,</font> <font color = "red">'Not Applicable; non-US demand'</font><font color = "maroon">)</font><font color = "silver">,</font> <font color = "red">''</font><font color = "maroon">)</font> <font color = "blue">AS</font> <font color = "maroon">[Visa Aging]</font><font color = "silver">,</font>
<br/>       <font color = "maroon">[onsite country]</font><font color = "silver">,</font>
<br/>       <font color = "maroon">[rrd id]</font><font color = "silver">,</font>
<br/>       <font color = "maroon">[ready to hard lock]</font><font color = "silver">,</font>
<br/>       <font color = "maroon">[account group]</font><font color = "silver">,</font>
<br/>       <font color = "maroon">[client]</font><font color = "silver">,</font>
<br/>       <font color = "maroon">[project]</font><font color = "silver">,</font>
<br/>       <font color = "maroon">Dem</font><font color = "silver">.</font><font color = "maroon">[start date]</font><font color = "silver">,</font>
<br/>       <font color = "maroon">Dem</font><font color = "silver">.</font><font color = "maroon">[end date]</font><font color = "silver">,</font>
<br/>       <font color = "maroon">[primary skill]</font><font color = "silver">,</font>
<br/>       <font color = "maroon">Dem</font><font color = "silver">.</font><font color = "maroon">[career level]</font>
<br/><font color = "blue">FROM</font>   <font color = "maroon">[demand$]</font> <font color = "blue">AS</font> <font color = "maroon">[Dem]</font>
<br/>       <font color = "blue">LEFT</font> <font color = "blue">JOIN</font> <font color = "maroon">[supply$]</font> <font color = "blue">AS</font> <font color = "maroon"></font>
<br/>              <font color = "blue">ON</font> <font color = "maroon">(</font> <font color = "maroon">Sup</font><font color = "silver">.</font><font color = "maroon">[skill]</font> <font color = "silver">=</font> <font color = "maroon">Dem</font><font color = "silver">.</font><font color = "maroon">[primary skill]</font> <font color = "maroon">)</font>
<br/><font color = "blue">WHERE</font>  <font color = "maroon">(</font> <font color = "maroon">Sup</font><font color = "silver">.</font><font color = "maroon">[skill type]</font> <font color = "blue">IS</font> <font color = "blue">NULL</font>
<br/>          <font color = "blue">OR</font> <font color = "maroon">Sup</font><font color = "silver">.</font><font color = "maroon">[skill type]</font> <font color = "silver">=</font> <font color = "red">'PRIMARY SKILL ASSESSMENT'</font> <font color = "maroon">)</font>
<br/>       <font color = "blue">AND</font> <font color = "maroon">(</font> <font color = "maroon">Sup</font><font color = "silver">.</font><font color = "maroon">[project tenure]</font> <font color = "blue">IS</font> <font color = "blue">NULL</font>
<br/>              <font color = "blue">OR</font> <font color = "maroon">Sup</font><font color = "silver">.</font><font color = "maroon">[project tenure]</font> <font color = "silver">=</font> <font color = "red">'>=12 months'</font> <font color = "maroon">)</font>
<br/>       <font color = "blue">AND</font> <font color = "maroon">Sup</font><font color = "silver">.</font><font color = "maroon">[gcp status]</font> <font color = "blue">IS</font> <font color = "blue">NULL</font>
<br/>       <font color = "blue">AND</font> <font color = "maroon">Sup</font><font color = "silver">.</font><font color = "maroon">[pip status]</font> <font color = "blue">IS</font> <font color = "blue">NULL</font>
<br/>       <font color = "blue">AND</font> <font color = "maroon">(</font> <font color = "maroon">Sup</font><font color = "silver">.</font><font color = "maroon">[rating - eligibility]</font> <font color = "blue">IS</font> <font color = "blue">NULL</font>
<br/>              <font color = "blue">OR</font> <font color = "maroon">Sup</font><font color = "silver">.</font><font color = "maroon">[rating - eligibility]</font> <font color = "silver">=</font> <font color = "red">'Yes'</font> <font color = "maroon">)</font>
<br/>       <font color = "blue">AND</font> <font color = "maroon">(</font> <font color = "maroon">Sup</font><font color = "silver">.</font><font color = "maroon">[accenture tenure]</font> <font color = "blue">IS</font> <font color = "blue">NULL</font>
<br/>              <font color = "blue">OR</font> <font color = "maroon">Sup</font><font color = "silver">.</font><font color = "maroon">[accenture tenure]</font> <font color = "silver">=</font> <font color = "red">'Yes'</font> <font color = "maroon">)</font>
<br/>       <font color = "blue">AND</font> <font color = "maroon">(</font> <font color = "maroon">Sup</font><font color = "silver">.</font><font color = "maroon">[career level]</font> <font color = "blue">IS</font> <font color = "blue">NULL</font>
<br/>              <font color = "blue">OR</font> <font color = "maroon">Sup</font><font color = "silver">.</font><font color = "maroon">[career level]</font> <font color = "blue">IN</font> <font color = "maroon">(</font><font color = "blue">SELECT</font> <font color = "maroon">[range level 1]</font>
<br/>                                        <font color = "blue">FROM</font>   <font color = "maroon">[matrix$]</font> <font color = "blue">AS</font> <font color = "maroon">[M]</font>
<br/>                                        <font color = "blue">WHERE</font>  <font color = "maroon">M</font><font color = "silver">.</font><font color = "maroon">[range level]</font> <font color = "silver">=</font> <font color = "maroon">Dem</font><font color = "silver">.</font><font color = "maroon">[career level]</font><font color = "maroon">)</font> <font color = "maroon">)</font>
<br/>       <font color = "blue">AND</font> <font color = "maroon">Dem</font><font color = "silver">.</font><font color = "maroon">[career level]</font> <font color = "silver">=</font> <font color = "red">'09-Team Lead'</font>
<br/><font color = "blue">ORDER</font>  <font color = "blue">BY</font> <font color = "maroon">[sap id]</font> <font color = "blue">DESC</font> 
</font>
 
Upvote 0
No this isn't it, if the WHERE Clause is not met then there won't be anything displayed. There's really a big difference between putting the criteria in the WHERE Clause and on the JOIN Clause. For example if I want to make all the Demands display and wants to LEFT JOIN it to the Supply and put a condition in the LEFT JOIN Clause then if that condition is not met the Demands won't be affected by it. If you put that condition in the WHERE Clause then the Demands will be affected.
 
Upvote 0
No they won't it's a left join; that's why there's a null check in each where criteria. The only mandatory where criteria is:
Rich (BB code):
Dem.[career level] = '09-Team Lead'
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,964
Members
452,371
Latest member
Frana

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