?
Solved

Query with IF's (SQL)

Posted on 2011-02-23
4
Medium Priority
?
274 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 93

Accepted Solution

by:
Patrick Matthews earned 1400 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 600 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 93

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

How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Suggested Courses

777 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