Frank Martin
Frank Martin

Reputation: 2594

Left Join works with table but fails with query

The following left join query in MS Access 2007

SELECT
Table1.Field_A,
Table1.Field_B,
qry_Table2_Combined.Field_A,
qry_Table2_Combined.Field_B,
qry_Table2_Combined.Combined_Field
FROM Table1
LEFT JOIN qry_Table2_Combined
 ON (Table1.Field_A = qry_Table2_Combined.Field_A) 
AND (Table1.Field_B = qry_Table2_Combined.Field_B);

is expected by me to return this result:

+--------+---------+---------+---------+----------------+
|Field_A | Field_B | Field_A | Field_B | Combined_Field |
+--------+---------+---------+---------+----------------+
|1       |         |         |         |                |
+--------+---------+---------+---------+----------------+
|1       |         |         |         |                |
+--------+---------+---------+---------+----------------+
|2       |1        |2        |1        |John, Doe       |
+--------+---------+---------+---------+----------------+
|2       |2        |         |         |                |
+--------+---------+---------+---------+----------------+

[Table1] has 4 records, [qry_Table2_Combined] has 1 record.

But it gives me this:

+--------+---------+---------+---------+----------------+
|Field_A | Field_B | Field_A | Field_B | Combined_Field |
+--------+---------+---------+---------+----------------+
|2       |1        |2        |1        |John, Doe       |
+--------+---------+---------+---------+----------------+
|2       |2        |2        |         |,               |
+--------+---------+---------+---------+----------------+

Really weird is that the [Combined_Field] has a comma in the second row. I use a comma to concatenate two fields in [qry_Table2_Combined].

If the left join query uses a table created from the query [qry_Table2_Combined] it works as expected.

Why does this left join query not give the same result for a query and a table? And how can i get the right results using a query in the left join?

Upvotes: 1

Views: 674

Answers (3)

onedaywhen
onedaywhen

Reputation: 57023

Concatenation: change the & operators to + operators and the result should be as expected.


Missing rows: I can reproduce this issue but cannot explain it, other than to say a) it's probably a bug and b) it will probably never get fixed :(

For sanity I tested the same code in SQL Server and it works as expected.

As a general point an outer join can be simulated using union and padding the missing values e.g. pseudo code:

( A JOIN B )
UNION
( A NOT MATCH B { A.*, <pad values for B> } )

In your case and in Access SQL:

SELECT Table1.Field_A, Table1.Field_B,  
       qry_Table2_Combined.Field_A,  
       qry_Table2_Combined.Field_B,  
       qry_Table2_Combined.Combined_Field  
  FROM Table1  
       INNER JOIN qry_Table2_Combined  
          ON (Table1.Field_A = qry_Table2_Combined.Field_A)  
             AND (Table1.Field_B = qry_Table2_Combined.Field_B)
UNION ALL
SELECT Table1.Field_A, Table1.Field_B,  
       NULL AS Field_A,  
       NULL AS Field_B,  
       NULL AS Combined_Field  
  FROM Table1  
 WHERE NOT EXISTS ( SELECT * 
                      FROM qry_Table2_Combined 
                     WHERE (Table1.Field_A = qry_Table2_Combined.Field_A)  
                            AND (Table1.Field_B = qry_Table2_Combined.Field_B) );

The above seems to produce the results you were expecting.


Access repro code, with concatenation fix, uncomment code for suggested workaround:

Sub EXfewfTempler()

  On Error Resume Next
  Kill Environ$("temp") & "\DropMe.mdb"
  On Error GoTo 0

  Dim cat
  Set cat = CreateObject("ADOX.Catalog")

  With cat
    .Create _
        "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=" & _
        Environ$("temp") & "\DropMe.mdb"

    With .ActiveConnection

      Dim Sql As String

      Sql = "CREATE TABLE Table1 (  Field_A VARCHAR(10),  Field_B VARCHAR(10) );"
      .Execute Sql

      Sql = "CREATE TABLE Table2 ( Field_B VARCHAR(10),  Col_1 VARCHAR(10),  Col_2 VARCHAR(10));"
      .Execute Sql

      Sql = "CREATE VIEW qry_Table2_Combined AS SELECT '2' AS Field_A, Table2.Field_B, Table2.Col_1 + ', ' + Table2.Col_2 AS Combined_Field FROM Table2; "
      .Execute Sql

      Sql = "INSERT INTO Table1 VALUES (1, NULL);"
      .Execute Sql
      Sql = "INSERT INTO Table1 VALUES (1, NULL);"
      .Execute Sql
      Sql = "INSERT INTO Table1 VALUES (2, 1);"
      .Execute Sql
      Sql = "INSERT INTO Table1 VALUES (2, 2);"
      .Execute Sql
      Sql = "INSERT INTO Table2 VALUES (1, 'John', 'Doe');"
      .Execute Sql

      Sql = _
          "SELECT " & _
          "Table1.Field_A, " & _
          "Table1.Field_B, " & _
          "qry_Table2_Combined.Field_A, " & _
          "qry_Table2_Combined.Field_B, " & _
          "qry_Table2_Combined.Combined_Field " & _
          "FROM Table1 " & _
          "LEFT JOIN qry_Table2_Combined " & _
          " ON (Table1.Field_A = qry_Table2_Combined.Field_A) " & _
          "AND (Table1.Field_B = qry_Table2_Combined.Field_B);"

'      Sql = _
'          "SELECT Table1.Field_A, Table1.Field_B, " & _
'          "       qry_Table2_Combined.Field_A, " & _
'          "       qry_Table2_Combined.Field_B, " & _
'          "       qry_Table2_Combined.Combined_Field " & _
'          "  FROM Table1 " & _
'          "       INNER JOIN qry_Table2_Combined " & _
'          "          ON (Table1.Field_A = qry_Table2_Combined.Field_A) " & _
'          "             AND (Table1.Field_B = qry_Table2_Combined.Field_B) " & _
'          "UNION ALL " & _
'          "SELECT Table1.Field_A, Table1.Field_B, " & _
'          "       NULL AS Field_A, " & _
'          "       NULL AS Field_B, " & _
'          "       NULL AS Combined_Field " & _
'          "  FROM Table1 " & _
'          " WHERE NOT EXISTS ( SELECT * " & _
'          "                      FROM qry_Table2_Combined " & _
'          "                     WHERE (Table1.Field_A = qry_Table2_Combined.Field_A) " & _
'          "                            AND (Table1.Field_B = qry_Table2_Combined.Field_B) );"


      Dim rs
      Set rs = .Execute(Sql)
      MsgBox rs.GetString(2, , vbTab & vbTab, , "<NULL>")

    End With
    Set .ActiveConnection = Nothing
  End With
End Sub

Upvotes: 1

Fionnuala
Fionnuala

Reputation: 91326

Looking at you logic, it seems that you only want combined fields where field_A = "2" (SELECT '2' AS Field_A). I suspect that this is causing the problem. Would it be possible to go about a solution in a different way, for example:

SELECT 
   t1.Field_A, 
   t1.Field_B, 
   t2.Field_B As t2B, 
   [t2].[Col_1] & ", " & [t2].[Col_2] AS Combined
FROM t1 LEFT JOIN t2 
ON t1.Field_B = t2.Field_B
WHERE t1.Field_A="2"
UNION ALL
SELECT  
   t1.Field_A, 
   t1.Field_B, 
   "None" As t2B, 
   "None" AS Combined
FROM t1 
WHERE t1.Field_A<>"2"

Upvotes: 3

ChrisPadgham
ChrisPadgham

Reputation: 870

isn't this a problem with MSAccess parsing. for a test change the field names in the query to Field_C and Field_D and see if you still have the same problem

Upvotes: 0

Related Questions