sql - Case query based on two input values -
i have 2 tables (fruits
, fruitsales
), query requirement is:
if fruit = 'apple' or 'banana' fire (query1) else fire(query2)
// i.e.., when input apple or banana, query1 has fire or else query2.
here 2 queries:
query #1:
select a.*, b.quantity fruits left join fruitsales b on a.fruitprice = '%'+b.fruitprice+'%' a.fruit = 'apple'
query #2:
select a.*, b.quantity fruits left join fruitsales b on a.fruitprice '%' + b.fruitprice + '%' a.fruit = 'orange'
in short: difference in query "like" in query2 , "=" in query1. don't know how use case queries in case.(as input data dependent on 2 values, apple or banana)
solution appreciated.
use case
condition in join
condition.
select a.*, b.quantity fruits left join fruitsales b on case when a.fruit in ('apple', 'banana') , a.fruitprice = '%'+b.fruitprice+'%' 1 when a.fruit not in ('apple', 'banana') , a.fruitprice '%' + b.fruitprice + '%' 1 else 0 end = 1 a.fruit = 'apple'
Comments
Post a Comment