Error with Inner Joins on 3 tables

pbassett

Active Member
Joined
May 5, 2004
Messages
358
I have 3 tables - the first 2 tables are linked by PIW and Ref, and the 1st and 3rd by PIW. The field Ref may be Null; hence the nz() addition in the following erroneous SQL:

SELECT [A].*, .*, [C].[status]
FROM [A]
INNER JOIN ON [A].[PIW]=.[PIW] AND
nz([A].[Ref],"")=nz(.[Ref],"")
INNER JOIN [C] ON [A].[PIW]=[C].[PIW]

I get the message "Syntax Error - Missing Operator".

I do know this 2-table query works:

SELECT [A].*, .*
FROM [A]
INNER JOIN ON [A].[PIW]=.[PIW] AND
nz([A].[Ref],"")=nz(.[Ref],"")

Any suggestions to get the first query to work?

Thanks,
Pete
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Are you just using SQL or working with the query designer?
 
Upvote 0
I have to use SQL only since the working INNER JOIN with the nz() has resulted in the Design Tool no longer being accessible.
 
Upvote 0
What do you wish to do when Ref is null?

I don't quite understand why you are using Nz.
 
Upvote 0
I could not get matches for a PIW and Ref if Ref was Null, since you can't Null fields to match in a SELECT. So by forcing Ref to " " via the nz() I can at least get matches. For example, I may have a PIW of 19010 and a Null Ref in the 2 tables. without nz() I wouldn't link them since the Null Ref fields would not match, as Null doesn't equal Null in SQL.
 
Upvote 0
I'm sure there is another way to do it but if it works it works.

BTW

The 'syntax error is being caused by a missing bracket

SELECT [A].*, .*, [C].[status]
FROM [A]
INNER JOIN ON [A].[PIW]=.[PIW] AND
(nz([A].[Ref],"")=nz(.[Ref],"")
INNER JOIN [C] ON [A].[PIW]=[C].[PIW]
 
Upvote 0
I got it to work putting parentheses after the FROM and after the first INNER JOIN, like this:

SELECT [A].*, .*, [C].[status]
FROM ([A]
INNER JOIN ON [A].[PIW]=.[PIW] AND
nz([A].[Ref],"")=nz(.[Ref],""))
INNER JOIN [C] ON [A].[PIW]=[C].[PIW]
 
Upvote 0

Forum statistics

Threads
1,221,713
Messages
6,161,459
Members
451,708
Latest member
PedroMoss2268

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