Solved

Query with IF's (SQL)

Posted on 2011-02-23
4
267 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
  • 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now