Norman Maina
asked on
sql query -select if statement
Using the below data as an example
ID Stage Reel GW NW
141 Intake 610356W007 156.76 158.20
183 Slitting 610356W007/3 32.17 32.77
140 Intake 610356W008 156.26 157.70
I want to select NW if Stage='Intake' or Select GW where Stage='Slitting'
How can i do that in a single SQL Query
ID Stage Reel GW NW
141 Intake 610356W007 156.76 158.20
183 Slitting 610356W007/3 32.17 32.77
140 Intake 610356W008 156.26 157.70
I want to select NW if Stage='Intake' or Select GW where Stage='Slitting'
How can i do that in a single SQL Query
Select *,
case when Stage= 'Intake' then NW
Else GW end
From table
i think that would do the job.
That's the correct one.
SELECT (CASE WHEN Stage='Intake' THEN NW WHEN Stage='Slitting' GW ELSE 0 END) FROM (TableName)
SELECT (CASE WHEN Stage='Intake' THEN NW WHEN Stage='Slitting' GW ELSE 0 END) FROM (TableName)
ASKER
beyazlale:>am getting "Incorrect syntax near the keyword 'WHEN'."
Select case stage when 'Intake' then NW when 'Slitting' then GW end as colname from tabname
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi,
Thats is also wrong beyzalate
SELECT
CASE Stage WHEN 'Intake' THEN NW
WHEN 'Slitting' THEN GW
ELSE Stage END as 'New Column'
FROM TableName
Thats is also wrong beyzalate
SELECT
CASE Stage WHEN 'Intake' THEN NW
WHEN 'Slitting' THEN GW
ELSE Stage END as 'New Column'
FROM TableName
can you pass the table script containing data also so that I can check the output also in sql editor.
Regarding your code.
SELECT
CASE WHEN Stage = 'Intake' THEN NW
ELSE CASE WHEN Stage = 'Slitting' THEN GW
ELSE Stage END END as 'New Column'
FROM TableName
SELECT
CASE WHEN Stage = 'Intake' THEN NW
ELSE CASE WHEN Stage = 'Slitting' THEN GW
ELSE Stage END END as 'New Column'
FROM TableName
Hi,
You can use any of below.
You can use any of below.
SELECT
CASE Stage WHEN 'Intake' THEN NW
WHEN 'Slitting' THEN GW
ELSE 0 END as 'New Column'
FROM TableName
SELECT
CASE WHEN Stage = 'Intake' THEN NW
ELSE CASE WHEN Stage = 'Slitting' THEN GW
ELSE 0 END END as 'New Column'
FROM TableName
ASKER
works perfect.Thanks
Hi Norman,
SELECT (CASE WHEN Stage='Intake' THEN NW WHEN Stage='Slitting' THEN GW ELSE 0 END) FROM (TableName)
THEN WAS MISSING.
SELECT (CASE WHEN Stage='Intake' THEN NW WHEN Stage='Slitting' THEN GW ELSE 0 END) FROM (TableName)
THEN WAS MISSING.
ASKER
Brichsoft:>hadnt seen your code - but it works.
Hey Norma....
its ok...if you didnt check....
As this Question might be stored in E.E. History so just telling Then was missing....
I write for newbie person..........
its ok...if you didnt check....
As this Question might be stored in E.E. History so just telling Then was missing....
I write for newbie person..........
SELECT (CASE WHEN Stage='Intake' THEN NW ELSE WHEN Stage='Slitting' GW ELSE 0 END) FROM (TableName)