Link to home
Start Free TrialLog in
Avatar of Norman Maina
Norman MainaFlag for Kenya

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
Avatar of beyazlale
beyazlale
Flag of Türkiye image

Try this.
SELECT (CASE WHEN Stage='Intake'  THEN NW ELSE WHEN Stage='Slitting' GW ELSE 0 END) FROM (TableName)


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)
Avatar of Norman Maina

ASKER

beyazlale:>am getting "Incorrect syntax near the keyword 'WHEN'."
Avatar of Sara bhai
Select case stage when 'Intake' then NW when 'Slitting' then GW end as colname from tabname
ASKER CERTIFIED SOLUTION
Avatar of beyazlale
beyazlale
Flag of Türkiye image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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
Hi,

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

Open in new window

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.



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..........