Solved

Query with IF's (SQL)

Posted on 2011-02-23
4
271 Views
Last Modified: 2012-05-11
I have a table with 5 fields.

FriendYN
EnemyYN
SellingPrice1
SellingPrice2
SellingPrice3

I want a query with 1 output field only.

The logic is as follows;

If EnemyYN="Y" and FRIENDYN<>"Y" select SellingPrice1
If FriendYN="Y" then select SellingPrice2
Else
Select sellingprice 3

How do I write this SQL?

(As a tangental question!  As a general rule would the experts prefer to receive a sample database attached with queries like this??  Is it more or less hassle to receive an attached database?)
0
Comment
Question by:Patrick O'Dea
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 350 total points
ID: 34965209
I like using Switch for this, as explained in http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_3556-Using-the-Switch-Function-in-Microsoft-Access.html


SELECT Switch(EnemyYN = "Y" And FriendYN <> "Y, SellingPrice1, EnemyYN = "Y", SellingPrice2, True, SellingPrice3) AS Result
FROM SomeTable

Open in new window



Getting a sample database is very often helpful, especially if there is any complexity to the request :)
0
 
LVL 9

Assisted Solution

by:borki
borki earned 150 total points
ID: 34965288
A sample database helps a lot - because in a scenario like yours, the best way to give you advise is by tweaking with a bit of data...

You could write a query field with IIF or SWITCH statements. However, i the logic gets complicated I prefer to write your own user defined function in VBA and call it from a query.

So the function would look like this

Public Function SellPrice(FriendYN As Variant, EnemyYN As Variant, Price1 As Variant, Price2 As Variant Price3 As Variant) As Variant
<insert your query code logic, as in the original question>
End Function

Then write a query such as:

SELECT SellPrice([FriendYN], [EnemyYN], [SellingPrice1], [SellingPrice2], [SellingPrice3]) AS Price
FROM <table>
WHERE <criteria>

HTH
0
 

Author Closing Comment

by:Patrick O'Dea
ID: 34965419
Thanks!

The switch is new to me!

Very useful too.
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 34965804
21Dewsbury,

Glad to help!  If you have not already done so, I would really appreciate it if you could please return to my article
http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_3556-Using-the-Switch-Function-in-Microsoft-Access.html
and click 'Yes' for the 'Was this helpful?' voting.

Patrick
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Check a field in Access on a Timer 1 33
Access syntax 1 35
MS Access How can I convert my VBA code from print preview to PDF output? 7 23
SQL Syntax 6 43
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

730 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