tags:

views:

583

answers:

7

For reasons beyond my control, I need to join two tables and I need null values to match. The best option I could think of was to spit out a UUID and use that as my comparison value but it seems ugly

SELECT * FROM T1 JOIN T2 ON nvl(T1.SOMECOL,'f44087d5935dccbda23f71f3e9beb491') = 
   nvl(T2.SOMECOL,'f44087d5935dccbda23f71f3e9beb491')

How can I do better? This is on Oracle if it matters, and the context is an application in which a batch of user-uploaded data has to be compared to a batch of existing data to see if any rows match. In retrospect we should have prevented any of the join columns in either data set from containing nulls, but we didn't and now we have to live with it.

Edit: To be clear, I'm not only concerned with nulls. If the columns are not null I want them to match on their actual values.

+4  A: 

Maybe this would work, but I've never actually tried it:

SELECT * 
FROM T1 JOIN T2 
ON T1.SOMECOL = T2.SOMECOL OR (T1.SOMECOL IS NULL AND T2.SOMECOL IS NULL)
Eric Petroelje
I haven't tried this either, but my brain keeps insisting you'd need a CROSS JOIN instead of a JOIN? I wouldn't trust it, though, it's been having to read through ugly Java code all day and is burnt out.
R. Bemrose
@R. Bemrose - Should only have a cross join type problem if you have multiple nulls. If there is only one null, it should work ok. But then again, if you have multiple nulls, there's really no solution since it would be impossible to decide which null to match to which other null.
Eric Petroelje
I was surprised, but that did in fact work.
Dan
+1  A: 

In SQL Server I have used:

WHERE (a.col = b.col OR COALESCE(a.col, b.col) IS NULL)

Obviously not efficient, because of the OR, but unless there's a reserved value you can map NULLs to on both sides without ambiguity or folding that's about the best you can do (and if there was, why was NULL even allowed in your design...)

Cade Roux
+2  A: 

You can't do any better, but the JOIN you have will not do an actual "JOIN" in any way (there won't be any correlation between T1.SOMECOL and T2.SOMECOL other than they both have a NULL value for that column). Basically that means that you won't be able to use a JOIN on NULLs to see if rows match.

NULL is never equal to another NULL. How can something of unknown value be equal to something else of unknown value?

Ken White
I know conceptually it can't. This particular feature of the application was designed stupidly. Basically we require the value of this column to be unique in the data set. That includes allowing there to be a row (but only one) with a null value. If the new data comes in and also has a null value for that column, we want that to match.
Dan
Not conceptually. In actuality. If the existing data has a key column that is null, and the new data has a key column that is null, they may +or may not+ match. IOW, given an existing row with a NULL key value, and 100 new rows each with a NULL key value, which single one matches that existing row? There's no such thing as a "unique NULL value" in the sense that your data is trying to use it. Or, more accurately, *all* NULLs are unique; factually speaking, no one NULL matches another.
Ken White
A: 

Just throwing this out there -- is there a way you could coalesce those nulls into a known value, like an empty string? Not knowing much about how your table is laid out means that I can't be sure if you'll be losing meaning that way -- i.e. having an empty string represent "user refused to enter a phone number" and NULL being "we forgot to ask about it", or something like that?

Odds are it's not possible, I'm sure, but if it is, you'll have known values to compare and you can get a legit join that way.

Jim Dagg
A: 

Isn't it the same as checking for presence of nulls in both columns?

SELECT * FROM T1, T2 WHERE T1.SOMECOL IS NULL and T2.SOMECOL IS NULL

or

SELECT * FROM T1 CROSS JOIN T2 WHERE T1.SOMECOL IS NULL and T2.SOMECOL IS NULL
Gambler
sql compares NULL with the "IS" keyword, not '=='. In fact, it doesn't use '==' anywhere, just plain '='.
Joel Coehoorn
You're right. Edited. But the point still stands. Isn't what the OP asked is effectively checking for null values in both columns? If you need to join on the other values as well, you can do (T1.SOMECOL IS NULL AND T2.SOMECOL IS NULL) OR (T1.SOMECOL = T2.SOMECOL).
Gambler
I find no reason for this answer to have -1 after being edited, so +1.
Hosam Aly
+1  A: 

Do you really want to be able to join the tables if a value is null? Can't you just exclude the possible null values in the join predicate? I find it hard to grok that rows in two tables can be related by a null value. If you have 100 nulls in table1.col_a and 100 nulls in table2.col_b, you're going to have 10000 rows returned just for the rows with null. It sounds incorrect.

However, you did say you need it. Can I suggest coalescing the null column into a smaller string as character comparisons are relatively expensive. Even better, coalesce the nulls into an integer if the data in the columns is going to be text. Then you have very quick 'comparisons' and you're unlikely to collide with existing data.

Josh Smeaton
A: 

For this sort of task Oracle internally uses an undocumented function sys_op_map_nonnull(), where your query would become:

SELECT *
FROM T1 JOIN T2 ON sys_op_map_nonnull(T1.SOMECOL) = sys_op_map_nonnull(T2.SOMECOL)

Undocumented, so be careful if you go this route.

David Aldridge