Solved

Query with IF's (SQL)

Posted on 2011-02-23
4
273 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

726 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