473,403 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

A CASE statement using another CASE statement as it's expression

Im trying to use 2 CASE statements together in a SELECT query without much luck.
The second CASE uses the output from the first CASE as it’s expression (well it’s supposed to).
When the 2nd CASE statement is removed it works, but when it is included i get this error:
Invalid column name 'balanceCalculation'.
Expand|Select|Wrap|Line Numbers
  1. SELECT accountNum
  2.  
  3. ,balanceCalculation = CASE
  4.     WHEN Balance > 5000 THEN Balance *2
  5.     ELSE Balance /2
  6. END 
  7.  
  8. ,balanceDescription = CASE 
  9.     WHEN balanceCalculation > 6000 THEN ‘Large Balance’
  10.     ELSE ’small balance’
  11. END
  12.  
  13.  
  14. FROM tblFinances
Feb 4 '10 #1

✓ answered by JonathanVH

You could use a common table expression, e.g.:
Expand|Select|Wrap|Line Numbers
  1. WITH BalCalc AS
  2. (SELECT AccountNum, CASE WHEN Balance > 5000 THEN Balance * 2 ELSE Balance / 2 END AS BalanceCalculation
  3.  FROM dbo.tblFinance)
  4. SELECT AccountNum, BalanceCalculation,
  5.  CASE WHEN BalanceCalculation > 6000 THEN 'Large Balance' ELSE 'Small Balance' END AS BalanceDescription
  6. FROM BalCalc;
To extend this:
Expand|Select|Wrap|Line Numbers
  1. WITH BalCalc AS
  2. (SELECT AccountNum, CASE WHEN Balance > 5000 THEN Balance * 2 ELSE Balance / 2 END AS BalanceCalculation
  3.  FROM dbo.tblFinance),
  4. BalDesc AS
  5. (SELECT AccountNum, BalanceCalculation,
  6.  CASE WHEN BalanceCalculation > 6000 THEN 'Large Balance' ELSE 'Small Balance' END AS BalanceDescription
  7.  FROM BalCalc)
  8. SELECT AccountNum, BalanceCalculation, BalanceDescription
  9. FROM BalDesc;

5 7224
ck9663
2,878 Expert 2GB
It would be better if you handle it on your front-end app.

Nevertheless, try this:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. SELECT accountNum,
  4.       balanceCalculation = 
  5.          CASE
  6.             WHEN Balance > 5000 THEN Balance * 2
  7.             ELSE Balance / 2
  8.          END,
  9.        balanceDescription =
  10.          case when 
  11.             CASE
  12.                WHEN Balance > 5000 THEN Balance * 2
  13.                ELSE Balance / 2
  14.             END > 6000 then ‘Large Balance’
  15.          else
  16.             'small balance
  17.          end
  18. FROM tblFinances
  19.  
  20.  
Happy Coding!!!

~~ CK
Feb 4 '10 #2
Hi, thanks for that ck, it works great :)

To take it one step further though (my example was very simple and stripped down), is it possible to store the output of the first case statement in a variable, then use that variable in other case statements later in the code? Its just that I have quite long cases, and they get used by other cases several times, so to repeat them inside other cases over and over is going to make the query massive.

If it's not possible then nevermind, the way you showed me works anyway so I can use that if nothing else, it would just be good to make things smaller instead of repeating the same code too many times.
Thanks again :)
Feb 5 '10 #3
ck9663
2,878 Expert 2GB
If you're thinking of using a variable inside the query and use that same variable in the same query, the answer is no. You can either use subquery or a scalar function instead.

Good Luck!!!


~~ CK
Feb 5 '10 #4
You could use a common table expression, e.g.:
Expand|Select|Wrap|Line Numbers
  1. WITH BalCalc AS
  2. (SELECT AccountNum, CASE WHEN Balance > 5000 THEN Balance * 2 ELSE Balance / 2 END AS BalanceCalculation
  3.  FROM dbo.tblFinance)
  4. SELECT AccountNum, BalanceCalculation,
  5.  CASE WHEN BalanceCalculation > 6000 THEN 'Large Balance' ELSE 'Small Balance' END AS BalanceDescription
  6. FROM BalCalc;
To extend this:
Expand|Select|Wrap|Line Numbers
  1. WITH BalCalc AS
  2. (SELECT AccountNum, CASE WHEN Balance > 5000 THEN Balance * 2 ELSE Balance / 2 END AS BalanceCalculation
  3.  FROM dbo.tblFinance),
  4. BalDesc AS
  5. (SELECT AccountNum, BalanceCalculation,
  6.  CASE WHEN BalanceCalculation > 6000 THEN 'Large Balance' ELSE 'Small Balance' END AS BalanceDescription
  7.  FROM BalCalc)
  8. SELECT AccountNum, BalanceCalculation, BalanceDescription
  9. FROM BalDesc;
Feb 6 '10 #5
Thats great, thanks very much to both of you, the info you provided has saved me a lot of time and given me other things to consider and research, much appreciated :)
Feb 8 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Chad Richardson | last post by:
I've always been mistified why you can't use a column alias in the group by clause (i.e. you have to re-iterate the entire expression in the group by clause after having already done it once in the...
8
by: JKop | last post by:
g++ test.cpp -ansi -pedantic -o test.exe test.cpp: In function `int main()': test.cpp:22: case label does not reduce to an integer constant Why won't "operator unsigned int() const" do its job...
21
by: Andy | last post by:
Can someone tell me if the following Switch...Case construct is valid? I'm wanting to check for multiple values in the Case statement without explicitly listing each values. So for example, will...
6
by: deanfamily11 | last post by:
I've set up a case statement to have my program determine where on the Cartesian plane a point the user enters is located. I keep getting the C2051 error when I compile. Any help? #include...
3
by: pgraeve | last post by:
I am a convert from VB to C# so bear with me on this "conversion" question C# switch statement seems to be the closest relative to VB's Select Case. I used VB's Select Case statement liberally. ...
25
by: CJM | last post by:
I'm getting a syntax error with a Select Case statement: Select Case CSng(rs.fields("Field1")) Case 0 Response.Write "Test1" Case Is < 0 <<< Syntax Error...
1
by: microsoft.public.dotnet.languages.vb | last post by:
Hi All, I wanted to know whether this is possible to use multiple variables to use in the select case statement such as follows: select case dWarrExpDateMonth, dRetailDateMonth case...
1
by: jelling | last post by:
Hi, Is it possible to use a CASE or IF ELSE statement in a datacolumn expression? Here's what I've tried: 'standardCodeColumn.Expression = "CASE StandardsBodyID WHEN 1 THEN 'JCAHO' ELSE...
4
by: Patrick A | last post by:
All, I rely on nested IF statements with multiple conditions heavily, and someone suggested recently writing the statements (and especially reading them months later) would be much easier if I...
56
by: Adem | last post by:
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15) says under...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.