简体   繁体   中英

Redshift left outer join is leaving out nulls

As background, I've set up 100's of redshift query's, many much more complex than this and yet I think I must be missing something simple.

I am doing a left outer join between Table 1 and Table 2. The tables are essentially this:

Table1
Col1  Col2 Col3
A     C    E
A     D    F

Table2
Col 1 Col2 Col3
A     C    Z

I have no where statements. My on statement is:

on Table1.Col1 = Table2.Col1 and Table1.Col2 = Table2.Col2

My result table is:

ResultTable:
Col1 Col2 Col3 Col4
A    C    E    Z

I was expecting:

ExpectedTable:
Col1 Col2 Col3 Col4
A    C    E    Z
A    D    F    Null

What am I missing? Thanks.

This may not be your problem, but Redshift doesn't match records using the equality operator if any of the columns used in the join are null, even if the column value is null on both sides of the join.

To handle this add an or condition for each join checking if both sides are null.

eg ((a.col1 = b.col1) or (a.col1 is null and b.col1 is null))

It seems that as of today, everything works as expected

WITH tst1 AS ( SELECT 'A' AS col1
                    , 'C' AS col2
                    , 'E' AS col3
            UNION ALL
               SELECT 'A' AS col1
                    , 'D' AS col2
                    , 'F' AS col3 )
                    
   , tst2 AS ( SELECT 'A'::VARCHAR AS col1
                    , 'C'::VARCHAR AS col2
                    , 'Z'::VARCHAR AS col3 )
                    
SELECT tst1.col1
     , tst1.col2
     , tst1.col3
     , tst2.col3 AS col4
FROM tst1
    LEFT JOIN tst2 ON tst1.col1 = tst2.col1
                  AND tst1.col2 = tst2.col2;
col1 col2 col3 col4
A C E Z
A D F

I think outer keyword is missing in your query. Here is an example of left outer join.

=> select * from Table1;
 col1 | col2 | col3
------+------+------
 A    | D    | F
 A    | C    | E
(2 rows)

=> select * from Table2;
 col1 | col2 | col4
------+------+------
 A    | C    | Z
(1 row)

=> select Table1.Col1, Table1.Col2, Table1.Col3, Table2.Col4 from Table1 left outer join Table2 on Table1.Col1 = Table2.Col1 and Table1.Col2 = Table2.Col2;
 col1 | col2 | col3 | col4
------+------+------+------
 A    | D    | F    |
 A    | C    | E    | Z
(2 rows)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM