Goodmorning everybody, I’m doing a query in Access 2010 and I’m getting a strange behavior, so I’d like to hear your opinion, here we go:
SELECT X.*, L.CodLinea
FROM (
SELECT TOP 1 'LBASE' AS CodLinea, 'Linea Base' AS Descrizione FROM ParametriAzienda
) AS X LEFT JOIN Linee AS L
ON X.CodLinea = L.CodLinea
The inner query gives always ONE row with two colums that should be something like:
CodLinea | Descrizione
----------+--------------
LBASE | Linea Base
Now, doing a left join like above should return ONE row with another column called L.CodLinea with a value that is EQUAL to ‘LBASE’ or NULL depending if the table Linee has got ‘LBASE’ inside. So, the result can be:
X.CodLinea | X.Descrizione | L.CodLinea
+-------------+-----------------+--------------+
LBASE | Linea Base | LBASE
if the value exists, or
X.CodLinea | X.Descrizione | L.CodLinea
+-------------+-----------------+--------------+
LBASE | Linea Base | null
if the value doesn’t exist. Indeed what I get is:
X.CodLinea | X.Descrizione | L.CodLinea
+-------------+-----------------+--------------+
LBASE | Linea Base | 0
LBASE | Linea Base | 0
LBASE | Linea Base | 0
LBASE | Linea Base | 0
LBASE | Linea Base | 0
That definitely can’t be possible, becasue: a) I should get only one row; b) 0 is NOT equal or like to LBASE. If I use the WHERE instead of LEFT JOIN (similar to an INNER JOIN) the or a RIGHT JOIN, the results are correct (0 rows).
What can be the matter?
EDIT: Actually ParametriAzienda has got only one row and Linee does not contain LBASE value.
The problem is
TOP 1: either you have misunderstood its use (bug in your code) or it’s a bug in the engine.Personally, I always use
DISTINCTbecause it is Standard SQL (and I understand its use 🙂 Indeed, when I tested this, replacingTOP 1withDISTINCTturns your actual result into the expected result.Here’s my repro. The below VBA creates a new data file in temp folder, with tables and sample data. No references required, simply copy+paste into any VBA module e.g. use Excel: