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
 Case Statement not working
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

accessdbguru
Starting Member

USA
26 Posts

Posted - 11/07/2012 :  17:03:58  Show Profile  Reply with Quote
I have the following CASE statement that needs to look at criteria on multiple columns and it is not working. Please help.


SELECT Invoice, Cost, Amount,
CASE Billed_Type
WHEN (dbo.SJC00999.Cost_Code_Number_1) ('LAK') THEN 'LAKA'
WHEN (dbo.SJC00999.Cost_Code_Number_2) ('LAK') THEN 'LAK1'
WHEN (dbo.SJC00999.Cost_Code_Number_3) ('UN') THEN 'UNBILLED'
WHEN (dbo.SJC00999.Cost_Code_Number_1) ('L3K') THEN 'LA3A'
ELSE 'BILLED'
END

FROM dbo.tbl52908

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 11/07/2012 :  17:12:17  Show Profile  Reply with Quote
You need to join the current FROM table that you have (dbo.tbl52908) to the table you are using in the case expression (dbo.SJC00999). How you would join depends on what column(s) can be used to join.

From the code you have written, I could not follow the logic you are trying to implement. Can you describe it?

Also, are you using Microsoft SQL Server? If not, this may not be the right forum.
Go to Top of Page

accessdbguru
Starting Member

USA
26 Posts

Posted - 11/07/2012 :  17:21:32  Show Profile  Reply with Quote
Yes I am using SQL Server 2005. Sorry i am using only 1 table in the query. Here is the REVISED SQL:

SELECT Cost_Code_Description AS Description,

CASE BilledType =
WHEN (dbo.SSJC00701.Cost_Code_Number_3) ('UN') THEN 'UNBILLED'
WHEN (dbo.SSJC00701.Cost_Code_Number_1) ('PINK') THEN 'PINKR'
ELSE
'BILLED'
END
AS
Billed_Type
FROM dbo.SSJC00701

Please advise the correct SQL Statement????

thank you.

Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 11/07/2012 :  17:36:16  Show Profile  Reply with Quote
SELECT Cost_Code_Description AS [Description],
       CASE 
            WHEN Cost_Code_Number_3 = 'UN' THEN 'UNBILLED'
            WHEN Cost_Code_Number_1 = 'PINK' THEN 'PINKR'
            ELSE 'BILLED'
       END AS Billed_Type
FROM   dbo.SSJC00701
The case expression is like an if clause - i.e., if Cost_Code_Number_3 = 'UN' is true, then it would return 'UNBILLED' regardless of whether or not Cost_Code_Number_1 = 'PINK'
Go to Top of Page

accessdbguru
Starting Member

USA
26 Posts

Posted - 11/07/2012 :  17:52:46  Show Profile  Reply with Quote
That works perfect! THANK YOU!!!!!!!!!!!!!!!!!!!!!!!!!!!
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 11/07/2012 :  20:02:20  Show Profile  Reply with Quote
You are very welcome.
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.07 seconds. Powered By: Snitz Forums 2000