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'.
- SELECT accountNum
-
-
,balanceCalculation = CASE
-
WHEN Balance > 5000 THEN Balance *2
-
ELSE Balance /2
-
END
-
-
,balanceDescription = CASE
-
WHEN balanceCalculation > 6000 THEN ‘Large Balance’
-
ELSE ’small balance’
-
END
-
-
-
FROM tblFinances
You could use a common table expression, e.g.: - WITH BalCalc AS
-
(SELECT AccountNum, CASE WHEN Balance > 5000 THEN Balance * 2 ELSE Balance / 2 END AS BalanceCalculation
-
FROM dbo.tblFinance)
-
SELECT AccountNum, BalanceCalculation,
-
CASE WHEN BalanceCalculation > 6000 THEN 'Large Balance' ELSE 'Small Balance' END AS BalanceDescription
-
FROM BalCalc;
To extend this: - WITH BalCalc AS
-
(SELECT AccountNum, CASE WHEN Balance > 5000 THEN Balance * 2 ELSE Balance / 2 END AS BalanceCalculation
-
FROM dbo.tblFinance),
-
BalDesc AS
-
(SELECT AccountNum, BalanceCalculation,
-
CASE WHEN BalanceCalculation > 6000 THEN 'Large Balance' ELSE 'Small Balance' END AS BalanceDescription
-
FROM BalCalc)
-
SELECT AccountNum, BalanceCalculation, BalanceDescription
-
FROM BalDesc;
5 7224
It would be better if you handle it on your front-end app.
Nevertheless, try this: -
-
-
SELECT accountNum,
-
balanceCalculation =
-
CASE
-
WHEN Balance > 5000 THEN Balance * 2
-
ELSE Balance / 2
-
END,
-
balanceDescription =
-
case when
-
CASE
-
WHEN Balance > 5000 THEN Balance * 2
-
ELSE Balance / 2
-
END > 6000 then ‘Large Balance’
-
else
-
'small balance
-
end
-
FROM tblFinances
-
-
Happy Coding!!!
~~ CK
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 :)
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
You could use a common table expression, e.g.: - WITH BalCalc AS
-
(SELECT AccountNum, CASE WHEN Balance > 5000 THEN Balance * 2 ELSE Balance / 2 END AS BalanceCalculation
-
FROM dbo.tblFinance)
-
SELECT AccountNum, BalanceCalculation,
-
CASE WHEN BalanceCalculation > 6000 THEN 'Large Balance' ELSE 'Small Balance' END AS BalanceDescription
-
FROM BalCalc;
To extend this: - WITH BalCalc AS
-
(SELECT AccountNum, CASE WHEN Balance > 5000 THEN Balance * 2 ELSE Balance / 2 END AS BalanceCalculation
-
FROM dbo.tblFinance),
-
BalDesc AS
-
(SELECT AccountNum, BalanceCalculation,
-
CASE WHEN BalanceCalculation > 6000 THEN 'Large Balance' ELSE 'Small Balance' END AS BalanceDescription
-
FROM BalCalc)
-
SELECT AccountNum, BalanceCalculation, BalanceDescription
-
FROM BalDesc;
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 :)
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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. ...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
| |