0

Table Structure

|id |location|sub-location|
--------------------------
|1| 70  |115|
|2| 70  |NULL|
|3| 70  |NULL|

Problem:

  1. If location is 70 and sub-location is 115. Query should return id = 1
  2. If sub-location is not equal to 115 (any other sub location) , query should return ids 2 and 3.
  3. Sub-location will come as parameter, if it exists return those specific rows otherwise only rows which contains null in sub location

I am using following query

Select id, location, sub-location
From table1 Where location = @location and (sub-location is null or sub-location = @sub-location)

Which will return id =1 if location is 70 and sub-location is 115. For other values of sub-location query will returns none of the rows which should not be the case. In case sub-location passed in query does not exist, query should return all the rows which matches with location whose sub-location is null

2
  • then what is your question what you want to retrive Commented Sep 25, 2014 at 3:59
  • @Sathish explained problem with existing query. Commented Sep 25, 2014 at 4:24

3 Answers 3

2

I think you want something like this:

       declare @sublocation int,
                        @location int
                   set @location = 70 
                   set @sublocation = 115

                select id
                from MyTable a
                where a.location = @location
                and ( a.sublocation  = @sublocation
                      or (a.sublocation is null and 
                        not exits (select 1 
                        from Mytable sub 
                        where sub.sublocation = @sublocation)
                    ) )
Sign up to request clarification or add additional context in comments.

3 Comments

Location and sub-location parameter will always pass, but if sub-location does not exist in table, all the rows with only null sublocation should returns null
Ok, changed it to that.
It seems better solution then mine.. +1
1

You can use CTE:

SQL Fiddle

    declare @sublocation int
    declare @location int

    set @location = 70

    set @sublocation=116

    ;with cte(id, location, sublocation)
    as
    (
    select id, location, coalesce(sublocation, -9999) as sublocation
    from table1
    where location=@location
    )

    select id, location, case when sublocation=-9999 then null else sublocation end from cte
    where
    sublocation =
      case when exists(select * from table1 where sublocation=@sublocation) then
          @sublocation
      else -9999
      end

The idea here, if sublocation does not exist convert it to -9999 or some illogical integer. Then use it in comparison. The reason is, we cannot compare both "is null" and "=" in one where clause using case statement

Comments

0

In spite of this request being quite old, I thought I might still give an answer :-)

First of all, the given query works different than described. It works fine for sub locations not found (i.e. different from 115 in the example) by getting only the NULL entries. But for a matching sub location (i.e. 115 in the example) it gets both the matching record and the NULL records, which is not desired.

And it is very easy to reduce the result for the latter case such that only the matching entry gets selected.

select top(1) with ties 
  id, location, sub_location
from table1 
where location = @location and (sub_location is null or sub_location = @sub_location)
order by case when sub_location is null then 2 else 1 end;

So all we do is order by whether the sub-location is null or not. Then we only keep the top group, i.e. the matching entry if any, or else the NULL entries.

By the way: Does the query with sub-location really work? It looks like a subtraction of two columns (sub and location). Does this work in SQL Server? Well, I've replaced the column name with sub_location (which conforms to the SQL standard) to make sure.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.