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

x
?
Solved

SQL Query If Statement

Posted on 2014-08-19
3
Medium Priority
?
231 Views
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.

Thanks,

wasmithpfs
0
Comment
Question by:wasmithpfs
3 Comments
 
LVL 36

Assisted Solution

by:ste5an
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 ,
        price
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

DECLARE @Fruits TABLE
    (
      FruitID INT ,
      FruitName NVARCHAR(255)
    );

INSERT  INTO @Fruits
VALUES  ( 1, 'Apple' ),
        ( 2, 'Orange' ),
        ( 3, 'Banana' );

DECLARE @Prices TABLE
    (
      FruitID INT ,
      PriceAmount MONEY
    );

INSERT  INTO @Prices
VALUES  ( 1, -1 ),
        ( 1, 0 ),
        ( 1, 1 ),
        ( 2, -2 ),
        ( 2, 0 ),
        ( 2, 2 ),
        ( 3, -3 ),
        ( 3, 0 ),
        ( 3, 3 );

SELECT  F.FruitName ,
        P.PriceAmount
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

0
 

Accepted Solution

by:
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,
        price
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'

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

Hope this helps
0
 

Author Closing Comment

by:wasmithpfs
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.


Regards,

Wasmithpfs
0

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