Sub Query with case statement - case-statement

SELECT
A.MARKETING_PLAN,
A.TERM,
A.TIER
CASE WHEN A.TERM<=60 THEN 'ST' ELSE 'LT' end as TERM_2
FROM ABC A;
I want to write the following case statement by using TERM_2 field(has 2 subset:LT and ST).
CASE WHEN TERM_2=LT and TIER=5 THEN 5.49 ELSE 'NULL' END AS BU
I can not use the above case statement as TERM_2 is just created.
I think I need to use sub query and I tried many ways but it does not work.
Please look at my code and help me solve the issue.
Many thanks in advance.

Because a.bu depends on value of a.term_2 and a.tier, it indirectly depends on a.term field. So you simply need to check whether a.term is greater than 60 (a.term_2 equals to LT if a.term is greater than 60).
SELECT
a.marketing_plan,
a.term,
a.tier,
CASE WHEN a.term<=60 THEN 'ST' ELSE 'LT' END AS term_2,
CASE WHEN a.term>60 AND a.tier=5 THEN 5.49 ELSE NULL END AS bu
FROM ABC a;

Related

T-SQL Nested Case Statement - Else Continue Nesting Case

In SQL Server, is there a way for nested case statements to ELSE continue the nesting CASE statement?
CASE
WHEN ... then
CASE
WHEN ... THEN
ELSE **[Continue to below WHEN]** END
WHEN ... then
WHEN ... then
ELSE ... END
Wondering if I can keep the code clean by not copy/pasting the proceeding WHEN statements into the nested CASE.
Flatten the nesting. So instead of this:
CASE
WHEN A then
CASE
WHEN B THEN C
WHEN D THEN E
ELSE **[Continue to below WHEN]** END
WHEN F then G
WHEN H then I
ELSE J END
You have this:
CASE
WHEN A AND B THEN C
WHEN A AND D THEN E
WHEN F then G
WHEN H then I
ELSE J END
A CASE expression (not statement: CASE cannot branch among SQL segments, only return a value) will stop at the first match, so by flattening the nesting and using and AND to tie in the nested conditions you will get the desired behavior.

case types boolean and double precision cannot be matched in Postgres

Query : if b is credit (i.e 0 or greater than 0), then the values should be a. Otherwise (a+b), and negative values should be zero.
My query condition is
Case when(b>=0) Then (a) Else (SUM(a+b)>0 End as Difference
But I'm getting this error
Error : CASE Types boolean and double precision cannot be matched
It would be a great pleasure if someone assist for the query solution. I'm using Postgres as database.
There is a syntax error in your expression; the number of opening and closing parentheses doesn't match.
But a is a number, while a + b > 0 is a boolean value (“true” or “false”), so PostgreSQL complains that it cannot determine what the type of your case expression should be.
According to your description, I would use
CASE WHEN b >= 0
THEN a
ELSE greatest(a + b, 0)
END

Ignore Condition on 1st Loop

if(Z1(m)<Z1(m-1)
IN=IN+1;
M1(:,IN)=Vb(:,t);
else
FCAST=Vb(N,T);
break;
end
I have this condition on my program, but the problem is it cannot work for first iteration because Z1(m)
Did anyone know how to solve this?
You can put the code you would like in the first iteration in it's own if statement
IF in == 1 || Z1(m)<Z1(m-1)
...
That might work.

Avoiding repetition when testing for empty fields in an if statement

I'm wondering if there is a way to avoid repeating myself in the code below:
if (isfield(A,'test') && isempty(A.test)) || ~isfield(A,'test')
statement1
else
statement2
end
alternatively, this is equivalent to:
if isfield(A,'test')
if isempty(A.test)
statement1
else
statement2
else
statement1
end
In the first example, I've repeated the isfield condition and in the second statement1 is repeated. Is there a neat way to do this without repetition?
Thanks,
Rich
Simply
if isfield(A,'test') && ~isempty(A.test)
statement2
else
statement1
end
is enough.
Because using the operator && the statement isempty(A.test) is just tested if isfield(A,'test') is true, otherwise it is skipped anyway.
expr1 && expr2 represents a logical AND operation that employs
short-circuiting behavior. With short-circuiting, the second operand
expr2 is evaluated only when the result is not fully determined by the
first operand expr1. For example, if expr1 = 0, then the following
statement evaluates to false, regardless of the value of expr2.
Testing:
A = struct % Case1
%A.test = 5 % Case2
%A = 5 % Case3
if isfield(A,'test')
if isempty(A.test)
disp(1)
else
disp(2)
end
else
disp(1)
end
if isfield(A,'test') && ~isempty(A.test)
disp(2)
else
disp(1)
end
For all 3 testing cases the results are the same.
I think either
if ~isfield(A,'test') || isempty(A.test))
statement1
else
statement2
end
or
if isfield(A,'test') && isempty(A.test)
statement2
else
statement1
end
will do what you need.
MATLAB's || and && operators short-circuit, so if the first operands evaluate to true (in the first case) or false (in the second case), the second operand is not evaluated and won't cause an error.
Don't think there's any other way, other than storing the condition in a boolean variable and passing it on, like this -
cond1 = isfield(A,'test');
if (cond1 && isempty(A.test) || ~cond1)
statement1
else
statement2
end
Though I must add, I would rather prefer the IF-ELSE style that you have adopted in your second approach. The case when 'test' field doesn't exist for A, it would throw error if you do only isempty(A.test), but with the double checking of that alongwith isfield(A,'test'), I think MATLAB ignores the error.

How to put " <" to this CASE WHEN expression?

In PostgreSQL this is a valid query:
SELECT case 2+2 when 1 then 2 else 3 end
If I put a complex subquery instead of '2+2' it still works well. But how can I change this query if i want to know if the result is smaller than a specific number?
For example this one doesn't work:
SELECT case 2+2 when > 1 then 2 else 3 end
There are two forms of the CASE statement in SQL, described in the PostgreSQL manual here.
One, which you are using, compares a particular expression to a series of values, like a switch statement in C or PHP:
CASE something WHEN 1 THEN 'hello' ELSE 'goodbye' END
The other is a more general set of branching conditions, like an if-elseif-else sequence, or PHP's switch(true). The above can also be written like this:
CASE WHEN something = 1 THEN 'hello' ELSE 'goodbye' END
So to use any comparison other than =, you need the "if-like" version. In your example:
SELECT CASE WHEN 2+2 > 1 THEN 2 ELSE 3 END
select case when 2+2 > 1 then this else that end