Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Case Statement?

Author  Topic 

boehnc
Starting Member

15 Posts

Posted - 2012-02-16 : 15:20:02
I have a query..already with select *,from table1

now I need to look at two columns, if they meet certain criteria, I need to pruduce a certain value in another column. For example:

Column1, Column2, Column3 (calculated column)
CABG, 145, 1
KPRO, 138, 0

So, if column1 = CABG, and Column2 > 120 (has to be a CABG), then Column3 = 1, if not (CABG greater than 120), then 0.

I will then have to do the same thing with KPRO with a value of > 100 in column2.

any help is appreciated.

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-02-16 : 15:24:32
As you correctly guessed, you can use a case expression in your select statement, for example:
SELECT
CASE
WHEN (column1 = 'CABG' AND Column2 > 120)
OR (column1 = 'KPRO' AND Column2 > 120) THEN 1
ELSE 0
END AS Column3
FROM YOURTable;
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-02-16 : 15:26:25
Not sure based on what you wrote...BUT


SELECT Column1, Column2
, CASE WHEN Column1 = 'CABG AND Column2 > 120 THEN 1 ELSE 0
WHEN Column1 = 'CABD' AND Column2 > 120 THEN 1 ELSE 0
WHEN Column1 = 'KPRO' AND Column2 > 100 THEN 1 ELSE 0
....
END AS Column3
FROM Table1



????

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

boehnc
Starting Member

15 Posts

Posted - 2012-02-16 : 16:02:08
awesome..thanks!
Go to Top of Page
   

- Advertisement -