[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


SQL Query If Statement

Posted on 2014-08-19
Medium Priority
Last Modified: 2014-08-19
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.


Question by:wasmithpfs
LVL 36

Assisted Solution

ste5an earned 1000 total points
ID: 40270101
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


Accepted Solution

Spt_Us earned 1000 total points
ID: 40270138
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

Author Closing Comment

ID: 40270433
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.



Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question