IF Clause

GreenWizard

Board Regular
Joined
Dec 8, 2013
Messages
106
Based on the table below how can I write a SQL Query to Select the Date, Color, AND Corresponding Number (Based on the rules below)

EXAMPLE

DateColorNumber1Number2Number3
1/1Red259
1/2Blue753
1/3Brown614

<tbody>
</tbody>

IF Color = Red
Then Query Column3 = Number2

If Color = Blue
Then Query Column3 = Number1

If Color = Brown
Then Query Column3 = Number3

If Color = <Anything Other Color>
Then Query Column3 = N/A


EXAMPLE OF DESIRED SQL OUTPUT

DateColorColumn3
1/2Blue7
1/3Brown4
1/4BlackN/A
1/1Red5

<tbody>
</tbody>


SQL QUERY CODE: ???

THANKS SO MUCH!!!!!

 
Thanks! I think I'm getting close, but I still can't figure it out.

Select Date, Color, IIf([Color]="Red",[Number2],IIF([Color]="Blue",[Number1],IIF([Color]="Brown",[Number3],"N/A") AS Column3
FROM ExampleTable

Doesn't seem to work...


Based On This Logic

IF Color = Red
Then Query Column3 = Number2

If Color = Blue
Then Query Column3 = Number1

If Color = Brown
Then Query Column3 = Number3

If Color = Any Other Color<anything other="" color="">
Then Query Column3 = N/A</anything>
 
Upvote 0

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
You are missing a few parentheses. For every "(", you must have a matching ")".
Code:
[COLOR=#333333]Select Date, Color, IIf([Color]="Red",[Number2],IIF([Color]="Blue",[Number1],IIF([Color]="Brown",[Number3],"N/A"))) AS Column3[/COLOR]
[COLOR=#333333]FROM ExampleTable[/COLOR]
By the way, are "Number1", "Number2", and "Number3" field names, or hard=coded values you want to return?
 
Upvote 0
TIP The database way: Avoid hard coding anything values when you can store it in a table.

A much dimplier way that i like to use is a Look up table. This also allows you to add, delete or edit colors without any changes required to your query.

The look up table would have a column for color and then one for the desired value.

This will allow you to do a simple query with a join on the color fields.


BONUS: This would also allo you to use a combo box to select the color at data entry time.
 
Upvote 0

Forum statistics

Threads
1,215,133
Messages
6,123,233
Members
449,092
Latest member
SCleaveland

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top