TehBoyan
TehBoyan

Reputation: 6890

Determine the specific field that is not equal in sql

I have a query that is somewhat equal to the following:

SELECT @address = a.Id FROM dbo.[Address] AS a, dbo.Item as u
    WHERE a.Name = u.Name 
            AND ISNULL(a.Number, '') = ISNULL(u.Number, '') 
        AND ISNULL(a.Floor, '') = ISNULL(u.Floor, '')
        AND ISNULL(a.Door, '') = ISNULL(u.Door, '')
        AND a.Zip = u.Zip
        AND u.ItemId = @id;

The whole idea is to find the addresses from dbo.Address that correspond to the dbo.Item table.

Now, the problem is that from this query I need to have the non-matching ones and determine the reason why they didn't match example: we cannot find the Name, or the Number, or the Floor etc. I have been trying to accomplish this by using consecutive SELECTs:

SELECT @addressId = a.Id FROM dbo.[Address] AS a, dbo.Item as u
        WHERE a.Name = u.Name 
            AND u.ItemId = @id;
        -- Name not found           
        IF @addressId IS NULL
        BEGIN
            SET @retval = 4
            RETURN @retval;
        END
        ELSE
        BEGIN
            SELECT @addressId = a.Id FROM dbo.[Address] AS a, dbo.Item as u
            WHERE a.Name = u.Name 
                AND a.Number = ISNULL(u.HusNr, '')
                AND u.ItemId = @id;
            -- Number not found
            IF @addressId IS NULL
            BEGIN
                SET @retval = 5
                RETURN @retval;
            END
... and so on

but this is slow and I come to think that there's got to be a more intelligent way to accomplish this.

NOTE(on Icarus's answer):

The problem with this approach is that it tries to join several fields at once. Sql tries to join all the fields and thus will return NULL for the whole row, and not for the specific columns. If we want the specific columns we would have to do something like:

SELECT a.Id , 
       case when a.Name is null then 1
       when a.Number is null then 2
       when a.Floor is null and a.Door is null and a.Zip is null then 3
       when a.Number is not null and a. Floor is not null and a.Door is not null and a.Zip is not null then 0 end as Reason
FROM dbo.Item u1 left join dbo.[Address] a 
       ON a.Name = u1.Name left join dbo.Item u2
       ON a.Number = u2.Number left join dbo.Item u3
       ON a.Floor = u3.Floor left join dbo.Item u4
       ON a.Door = u4.Door left join dbo.Item u5
       ON a.Zip = u5.Zip
where  u.ItemId = @id;

...but then we get a bunch of results that we don't need i.e. all the possible combinations, which again...we don't need. and if we use the above CASE on that the first one is always true because the first result is all NULLs

Upvotes: 2

Views: 1007

Answers (2)

Icarus
Icarus

Reputation: 63962

You could return the reason code in a separate column as so:

SELECT a.Id , 
       case when a.Name is null then 1
       when a.Number is null then 2
       when a.Floor is null and a.Door is null and a.Zip is null then 3
       when a.Number is not null and a. Floor is not null and a.Door is not null and a.Zip is not null then 0 end as Reason
FROM dbo.[Address] a left join dbo.Item u
       on a.Name = u.Name 
       or a.Number = u.Number
       or a.Door = u.Door 
where  u.ItemId = @id;

Where Reason 0 above is a perfect match. You can define your reasons as you wish, my intention was to give you an idea on how to do this using a case.

Upvotes: 6

Maess
Maess

Reputation: 4156

Is there a reason you are trying to do this one address at a time?

You could find all the address that are null by using a LEFT OUTER join between address and item adding Address = NULL to your WHERE clause.

Upvotes: 0

Related Questions