SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 alternative to the Case Statement
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

yadhu_cse
Constraint Violating Yak Guru

India
252 Posts

Posted - 12/23/2011 :  01:21:46  Show Profile  Reply with Quote
Hi,

I have one scenario where i use following query
select number
case WHEN 1 Then 'good'
when 2 then 'avg'
when 3 then 'poor'
end as performance from test.

is there any alternative to above query where we can eleminate case statement.

khtan
In (Som, Ni, Yak)

Singapore
17440 Posts

Posted - 12/23/2011 :  01:34:23  Show Profile  Reply with Quote
might be. . . but what's wrong with that query ?


KH
Time is always against us

Go to Top of Page

yadhu_cse
Constraint Violating Yak Guru

India
252 Posts

Posted - 12/23/2011 :  01:57:00  Show Profile  Reply with Quote
Nothing wrong but i am trying to write query without case statement
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 12/23/2011 :  01:57:05  Show Profile  Reply with Quote
you can eliminate case by putting a mapping table with two fields like (perfid,perfmeasure)
and you can put values inside it

then above CASE statement can be dispensed with and instead you'll add a join to table created as

INNER JOIN MappingTable mt
ON mt.perfid = number
...

and in your select include mt.perfmeasure instead of CASE..WHEN

by implementing it as above its scalable also as for for each new measure value addition does not require you to modify the procedure but just need to add a new insert statement to add the value to table

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

yadhu_cse
Constraint Violating Yak Guru

India
252 Posts

Posted - 12/23/2011 :  02:04:16  Show Profile  Reply with Quote
you menat to say, i should create another table and add the value like performance,id..
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 12/23/2011 :  03:13:17  Show Profile  Reply with Quote
yep...if you want to make solution scalable and avoid putting a series of case expressions. once expression number goes beyond a limit table approach might work faster if you've proper indexes on table created

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Sachin.Nand
Flowing Fount of Yak Knowledge

2937 Posts

Posted - 12/23/2011 :  05:04:58  Show Profile  Reply with Quote
quote:
Originally posted by yadhu_cse

Hi,

I have one scenario where i use following query
select number
case WHEN 1 Then 'good'
when 2 then 'avg'
when 3 then 'poor'
end as performance from test.

is there any alternative to above query where we can eleminate case statement.



IIF....but for that you have to wait for SQL Server 2012 release.

PBUH

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000