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
 General SQL Server Forums
 New to SQL Server Programming
 Need to streamline "CASE" code from multiline to 1
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

jaimealvarez
Starting Member

29 Posts

Posted - 12/14/2012 :  15:23:48  Show Profile  Reply with Quote
Hi,

I have the code below

, CASE
WHEN matter.mrtcode = 10 then (select timerate.tkrt10 FROM son_db.dbo.timerate timerate INNER JOIN ( Select tkinit, MAX(tkeffdate) AS tkeffdate FROM son_db.dbo.timerate GROUP BY tkinit) timerate2 ON timerate.tkinit = timerate2.tkinit and timerate.tkeffdate = timerate2.tkeffdate WHERE (timerate.tkinit=timecard.ttk) AND (timerate.tkrtcur='USD')AND (timerate.tkeffdate>={ts '2012-01-01 00:00:00'}))
WHEN matter.mrtcode = 11 then (select timerate.tkrt11 FROM son_db.dbo.timerate timerate INNER JOIN ( Select tkinit, MAX(tkeffdate) AS tkeffdate FROM son_db.dbo.timerate GROUP BY tkinit) timerate2 ON timerate.tkinit = timerate2.tkinit and timerate.tkeffdate = timerate2.tkeffdate WHERE (timerate.tkinit=timecard.ttk) AND (timerate.tkrtcur='USD')AND (timerate.tkeffdate>={ts '2012-01-01 00:00:00'}))
end AS 'BaseRate'

This works fine but now I have to many lines, it goes from 10 to 50... so it is too long.

All lines are the same excpet for the numbers (matter.mrtcode = 10 and timerate.tkrt10) ... so what I have to do is in the case where the code is equal to 10 it has to look for the field that ends in 10, this is the same all the way down to 50!

Is there a way to make this a one-liner?

Any help would be appreciated. Thanks

tkizer
Almighty SQL Goddess

USA
35951 Posts

Posted - 12/14/2012 :  15:36:48  Show Profile  Visit tkizer's Homepage  Reply with Quote
Why is your table designed this way? Fix the design and this coding issue goes away.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 12/15/2012 :  13:03:13  Show Profile  Reply with Quote
sounds like unpivot scenario for me!

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

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.06 seconds. Powered By: Snitz Forums 2000