Thursday, 19 September 2013

mssql - select from another table if no results from the first table

mssql - select from another table if no results from the first table

I have to run a query to find a price. I have multiple price lists, each
in their own table. customer_price, default_price and minimum_price.
not all price lists have all the products and at times a product might not
appear on any price list and then the price needs to return 0 for the
price.
so I want to:
select price from customer_price where customer='walmart' and
product='whitebread'
if this returns a result then all is well. if the result is 0,NULL or the
query returns no rows I want this to run:
select price from default_price where customer='walmart' and
product='whitebread'
if this returns a result then all is well. if the result is 0,NULL or the
query returns no rows I want to simply return 0.
I am not sure how to proceed. I tried a case statement but the case
statement fails if no row is found in the results. how can I do this or
say if 0 results then
thanks in advance, as always.

No comments:

Post a Comment