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

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Date[/TD]
[TD]Color[/TD]
[TD]Number1[/TD]
[TD]Number2[/TD]
[TD]Number3[/TD]
[/TR]
[TR]
[TD]1/1[/TD]
[TD]Red[/TD]
[TD]2[/TD]
[TD]5[/TD]
[TD]9[/TD]
[/TR]
[TR]
[TD]1/2[/TD]
[TD]Blue[/TD]
[TD]7[/TD]
[TD]5[/TD]
[TD]3[/TD]
[/TR]
[TR]
[TD]1/3[/TD]
[TD]Brown[/TD]
[TD]6[/TD]
[TD]1[/TD]
[TD]4[/TD]
[/TR]
</tbody>[/TABLE]

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

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Date[/TD]
[TD]Color[/TD]
[TD]Column3[/TD]
[/TR]
[TR]
[TD]1/2[/TD]
[TD]Blue[/TD]
[TD]7[/TD]
[/TR]
[TR]
[TD]1/3[/TD]
[TD]Brown[/TD]
[TD]4[/TD]
[/TR]
[TR]
[TD]1/4[/TD]
[TD]Black[/TD]
[TD]N/A[/TD]
[/TR]
[TR]
[TD]1/1[/TD]
[TD]Red[/TD]
[TD]5[/TD]
[/TR]
</tbody>[/TABLE]


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

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
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,223,445
Messages
6,172,177
Members
452,446
Latest member
walkman99

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