tags:

views:

64

answers:

2

I'm trying to return the results of a query where two columns columns are not equal to each other.

However, when I run the query the results don't seem to be consistent with the actual data.

As you can see below, I want to return rows where the RatePlanIDs are different.

RatePlans_Supplemental.PIDs could actually be null, but I never see null results. It also returns duplicates.

I've tried this with muliple queries and they all seem to do the same thing.

I've actually verified it's returning wrong data. It's like the query is inserting values if there's a null. Any idea why?

Update Billing_UnitUsage
Set SuppRateSuccess = 0
FROM         Billing_UnitUsage INNER JOIN
                      RatePlans_Supplemental ON Billing_UnitUsage.SupplementalRateID = RatePlans_Supplemental.UDC_ID AND 
                      NOT(Billing_UnitUsage.RatePlanID = RatePlans_Supplemental.PIDs)
+4  A: 

Null is "unknown". Any function against unknown (including NOT) returns unknown. In order to get a row to return, your criteria must return TRUE.

Use IS NULL and IS NOT NULL to test for NULL.

Here's my analogy.

If there are two strangers in the room, and you ask, are their names the same? The answer is unknown.

If you ask, are their names not the same? The answer is unknown.

Without knowing their names, the answer, unfortunately, is always unknown.

Marcus Adams
+1 - If you don't have a good ***business reason*** to allow nulls, don't allow them, or convert them to empty strings/0's.
JNK
Many people believe NULL means empty, but it doesn't. It means "unknown". As @JNK pointed out, use empty for empty.
Marcus Adams
Great information. Thanks!
Infotech
@Marcus Adams: NULL is a placeholder for a value that is missing. The reason why it is missing may be "unknown" but there are many other possibilities e.g. "a value is not applicable", "the person is known to lack a middle name", "the period has not ended yet", "the witness pleaded the Fifth", etc.
onedaywhen
@JNK: I believe that an empty string value has no place in a database. IIRC Oracle treats `NULL` and empty string the same anyhow.
onedaywhen
@OneDayWhen - to me it depends (like nulls) on if there is a business reason for it. For instance I deal with medical chart data. Some fields are left blank intentionally (which would be an empty string), some are NULL for other reasons. The difference is important.
JNK
@JNK: "The difference is important" -- that we can agree on. However, I don't think introducing empty string and NULL into the domain values is a good way to model these important differences. I don't think we'll resolve our difference in comments. Peace :)
onedaywhen
+1  A: 

I added the table aliases just to improve readability.

If you don't want to update SuppRateSuccess when PIDs is NULL:

Update bu
Set SuppRateSuccess = 0
    FROM Billing_UnitUsage bu
        INNER JOIN RatePlans_Supplemental rs
            ON bu.SupplementalRateID = rs.UDC_ID 
    WHERE bu.RatePlanID <> ISNULL(rs.PIDs, bu.RatePlanID)

If you do want to update SuppRateSuccess when PIDs is NULL (assumes RatePlanID<>0):

Update bu
Set SuppRateSuccess = 0
    FROM Billing_UnitUsage bu
        INNER JOIN RatePlans_Supplemental rs
            ON bu.SupplementalRateID = rs.UDC_ID 
    WHERE bu.RatePlanID <> ISNULL(rs.PIDs, -bu.RatePlanID)
Joe Stefanelli
Thanks for the information. I never thought about it before reading this, but I guess I really don't have to set the value when it fails. It just won't be set and I can run a query based on that. You're also right about the table aliases. I took over this project and the original developer didn't use them, but I'm slowly updating them when I don't get lazy...
Infotech