Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use a Case to return 2 field values 1

Status
Not open for further replies.

scc

Programmer
Apr 30, 2001
218
US
Okay, I'm drawing a blank on this one.

I need to check several fields in the database and based on the criteria, return 2 fields in my select statement.

For instance, I have this CASE statement (this is just a small sampling of it):
CASE --Determine the Category
WHEN Disposition = 'GROUP' and Error_Code is null
THEN 'Group'

WHEN Disposition ='UNREPRICED' and Error_Code is null
THEN 'Provider'

WHEN Disposition = 'AUDITOR' and Error_Code is null
THEN 'Auditor'

ELSE 'Ready'
END AS Category

But I would also like to return a field called 'Stage' as well. In other words, all records with a Category of 'Group' would have a 'Stage' = 'Processed', all records with a Category of 'Provider' would have a 'Stage'= 'Complete' and all records with a Category of 'Auditor' or 'Ready' need to return a 'Stage' = 'Outbound'

What is the best way to do this?
 
Just have two case statements one after the other


CASE --Determine the Category
WHEN Disposition = 'GROUP' and Error_Code is null
THEN 'Group'

WHEN Disposition ='UNREPRICED' and Error_Code is null
THEN 'Provider'

WHEN Disposition = 'AUDITOR' and Error_Code is null
THEN 'Auditor'

ELSE 'Ready'
END AS Category,

CASE --Determine the Category
WHEN Disposition = 'GROUP' and Error_Code is null
THEN 'Processed'

WHEN Disposition ='UNREPRICED' and Error_Code is null
THEN 'Complete'


ELSE 'Outbound'
END AS Stage


Cheyney
 
Thanks for responding.

I thought of that, but the Case statement is sooo long and detailed, I was wondering if there was a better way so all this code didn't have to be repeated. I know cut and paste is easy, but it makes the maintenance much more difficult if someone else has to inherit and support the code.

 
You can embed the case [bold]expression[/bold] in a derived table, and thus have it only once.

Code:
select c1, c2, case c2 when 'Group' then 'Processed'
                       when ... 
                       when ... end 
from ( select c1 , CASE --Determine the Category
        WHEN Disposition = 'GROUP' and Error_Code is null
        THEN 'Group'
        WHEN Disposition ='UNREPRICED' and Error_Code is null
        THEN 'Provider'
        WHEN Disposition = 'AUDITOR' and Error_Code is null
        THEN 'Auditor'
        ELSE 'Ready'
    END AS C2 from t 
   where ...
) as dt
 
swampBoogie,

Ok, think I'm following you.

I have never used a derived table. Is "as dt" the derived table name, and is this like a temp table? Do I need to destroy it or anything?

And do I need to reference dt in anyway? If not, then what is the purpose of the "as dt" statement.

Thanks for your help. Again, this is something completely new to me.


 
Yes dt is the (arbitrary) name of the derived table. It must be named even though you don't use the name anywhere. There is no need to destroy anything, that is handled by the server (when necessary).
 
Thanks! This will be much easier to maintain.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top