SQL Query If Statement

Good Day,

I need to  write a  SQL query that goes something  like this:

Select  [apples], [oranges], {bananas],  [price] from Fruit_Tabletable where
 if [price]  >0, then [apples] =Yes and oranges= Yes

I need to only show apples and oranges  where the cost is  grater than zero and have "Yes" appear in the apples and oranges fields .

Please advise.


Wm Allen SmithAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
I need to only show apples and oranges  where the cost is  grater than zero and have "Yes" appear in the apples and oranges fields .
SELECT  apples ,
        oranges ,
        bananas ,
FROM    Fruit_Tabletable
WHERE   price > 0
        AND apples = 'Yes'
        AND oranges = 'Yes';

Open in new window

But columns named like fruits makes no sense in a normal model. E.g. a better model

      FruitID INT ,
      FruitName NVARCHAR(255)

VALUES  ( 1, 'Apple' ),
        ( 2, 'Orange' ),
        ( 3, 'Banana' );

      FruitID INT ,
      PriceAmount MONEY

VALUES  ( 1, -1 ),
        ( 1, 0 ),
        ( 1, 1 ),
        ( 2, -2 ),
        ( 2, 0 ),
        ( 2, 2 ),
        ( 3, -3 ),
        ( 3, 0 ),
        ( 3, 3 );

SELECT  F.FruitName ,
FROM    @Prices P
        INNER JOIN @Fruits F ON F.FruitID = P.FruitID
WHERE   P.PriceAmount > 0
        AND F.FruitName IN ( 'Apple', 'Orange' );

Open in new window

results in
FruitName	PriceAmount
---------	---------------------
Apple		1,00
Orange		2,00

Open in new window

Not 100% sure what you are asking here:

Select  [apples], [oranges], {bananas],  [price] from Fruit_Tabletable where
 if [price]  >0, then [apples] =Yes and oranges= Yes

With the "then [apples] = Yes and Oranges=Yes  piece;

1. Are you wanting to 'set' these as yes? Or
2. Make sure that "IF" the price is greater than 0 there 'has' to be a YES as well???

if #1 answer is yes:

You will need a 'case' statement in your select
SELECT  apples ,
        oranges ,
        bananas ,
       CASE Fruit
          When 'apples' THEN 'YES'
          When 'oranges' THEN 'YES'
      END AS Fruit_Type,
FROM    Fruit_Tabletable
WHERE   price > 0
        AND apples = 'Yes'
        AND oranges = 'Yes'

if #2 answer is yes; then
you just need a select where you 'WHERE' clauses are
WHERE Price > 0
AND apple = 'Yes'
AND oranges = 'yes'

WHERE Price > 0
AND (apple = 'Yes' OR oranges = 'yes' )

Hope this helps

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Wm Allen SmithAuthor Commented:
Thank you for your quick response, Spt_Us . " Are you wanting to 'set' these as yes? " is the scenario that I was aiming for and your solution works perfectly.


It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.