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

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

Related

MATLAB: Run multiple cases in switch

Is there a method for running multiple case statements (say 2 out of 3) in a MATLAB switch? Or do I have to use a series of if statements? I would like do something similar to:
test = {'test1','test2'}
switch test
case 'test1'
disp('test1')
case 'test2'
disp('test2')
case 'test3'
disp('test3')
end
Output:
test1
test2
On a side note: is there any way to parallelize such code so that the different cases can be run simultaneously?
the if statement would be more appropriate if you 1/ want to test for multiple cases 2/parallelize.
something like
if ismember('test1',test)
%code
end
if you want to make it parallel, you can do it through the following:
test is your data, case is the cell containing all possiblities
parfor(i=1:length(cases)){ %you need to parse the cases not the data
if(ismember(case{i},test)){
%code
}
}
A solution can be put the switch into a function and then use the cellfun. Hence, define the function:
function a = func(test)
switch test
case 'test1'
disp('test1')
case 'test2'
disp('test2')
case 'test3'
disp('test3')
end
end
then, apply to the test:
cellfun(#func, test)
The result would be:
test1
test2

Numbers on telephone to letters. 2=ABC 3=DEF etc. Using switch case but code isnt giving desired results. How would I return the code as asked below

for var=1:length(str)
switch str(var)
case {'A':'C'}
disp('2')
case {'D':'F'}
disp('3')
case {'G':'I'}
disp('4')
case {'J':'L'}
disp('5')
case {'M':'O'}
disp('6')
case {'P' 'R' 'S'}
disp('7')
case {'T':'V'}
disp('8')
case {'W':'Y'}
disp('9')
end
I am using this code in trying to make a statement such as ('1-800-TO-WELLS') become '1-800-86-93557', but the output is "str=1-800-TO-WELLS and 7". Any amount of tips and help is very appreciated. If I haven't provided a valid enough question please leave comments so I can improve.
For strings, the cases in the switch block act like calls to strcmp, so case <caseExpression> is true if any(strcmp(<caseExpression>,<switchExpression>)).
This is important because {'P' 'R' 'S'} and {'P':'S'} do not generate the same output:
>> {'P' 'R' 'S'}
ans =
'P' 'R' 'S'
>> {'P':'S'}
ans =
'PQRS'
The first is a 1-by-3 cell array with individual characters as the elements' contents; the second is a 1-by-1 cell array with a 1-by-4 character array as the element's content. Performing a strcmp on the first one will give true if the <switchExpression> is a letter in the set while the second will only give a true if <switchExpression> is exactly 'PQRS':
>> strcmp({'P' 'R' 'S'},'S')
ans =
0 0 1
>> strcmp({'P':'S'},'S')
ans =
0
>> strcmp({'P':'S'},'P':'S')
ans =
1
So the 7 pops out because its case is the only one that gives true when given a matching character.
As a side note, you may consider using the otherwise statement to echo non-matching characters:
switch str(var)
.
.
.
otherwise
disp(str(var));
end
Troy gives a very clear answer as to why your code isn't working: {'A':'C'} results in a 1-element cell array {'ABC'}, but you want to use a 3-element cell array {'A' 'B' 'C'} since the way the switch compares case expressions will result in a match if one cell element exactly matches a given character.
As an alternative to using a for loop and switch statement, you could also do this using the ismember function and some indexing:
% Character/number map and test string:
map = ['ABCDEFGHIJKLMNOPRSTUVWXY'; '222333444555666777888999'];
str = '1-800-TO-WELLS';
% Convert test string:
[isInMap, index] = ismember(str, map(1, :));
str(isInMap) = map(2, index(isInMap));
And the result:
str =
1-800-86-93557

Sub Query with 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;

How to use inputs in an if statement in matlab

I'm trying to write a code in MATLAB that has the user input two values. I already have everything written for the input part and I saved the two inputs into two variables: value1 and value2.
What I'm trying to do is use the input values in the matter of:
if value1 = 2
output_result=10
if value1 = 3
output_result=20
and so on.
I've been trying to write an if-elseif statement but I can't seem to figure it out.
Do a switch statement
switch value1
case 2
result = 10;
case 3
result = 20;
...
otherwise
statements
end
If you really want to use an if statement, do this:
if value == 1
result = 10;
elseif value == 2
result = 20;
elseif
%// Put more statements
...
elseif
%// Put even MOAR statements
...
...
else
%// Default case - optional
end
However, the switch statement as per #kkuilla is more elegant. Also note that the else statement is optional. You'd only put this in if everything else fails and want to use a default case.

Basic Syntax vs SQL Command -- Formula Crystal Reports

I am trying to convert this statement into a SQL command-- for testing and learning purposes.
The code in a formula in basic syntax is
if IsNumeric (Mid(Trim(X), 1)) Then
formula = UCASE(TRIM(MID(X, (INSTR (X, " ", 1) +1)))))
ELSE
formula = X.
What am I supposed to use instead of formula in the sql command and should this be done in sql command for efficiency? Even if I am not supposed to, I would like to know how to write this in sql command.
So far I have
CASE ISNUMERIC (Mid(Trim(X), 1)) Then
[BLANK] = UCASE(TRIM(MID(X, (CHARINDEX (X, " ", 1) +1)))))
ELSE
[BLANK] = X
EPV
Sample of Data. So when I import/link data from the back end. My X Field looks like a variety of:
1. 'zzabc123 - The Red Car'
2. 'abc123 - The Black Car'
3. 'The green car'
I want to create a SQL code where:
CHECK X (zzabc123 - the red car) to see if there is a zzabc123 in the front
if it is numeric, then cut out the front part - by using charindex to find where the space starts and grab only 'the red car'.
If it is not numeric, just clean up the DATA part
END CASE
After that case evaluate the DATA and use another CASE to find key words to use a generalized term for reporting.
RESULTS
RED CAR
BLACK CAR
GREEN CAR
SELECT *,
CASE ISNUMERIC(Mid(Trim(X), 1)) Then
UCASE(TRIM(MID(X, (CHARINDEX (X, " ", 1) +1)))))
ELSE X
END
CASE WHEN X LIKE '%RED%'
THEN 'RED CAR'
ELSE X LIKE '%BLACK%;
THEN 'BLACK CAR'
ELSE X LIKE '%GREEN%'
THEN 'GREEN CAR'
ELSE XY
END
FROM XTABLE`
Without knowing the specifics of your data and what you are trying to accomplish, it's next to impossible to accurately give you a SQL sample.
That said, here is a conversion of most of your functions to their SQL-Server equivalents; if you can look up items like the SUBSTRING() and CHARINDEX() functions, and adapt the sample to do whatever it is your requirements dictate, this should get you started.
SELECT
CASE
WHEN ISNUMERIC(SUBSTRING(LTRIM(RTRIM(X)), 1,1)) = 0
THEN UPPER(LTRIM(RTRIM(SUBSTRING(X, CHARINDEX(X, ' ')+1, LEN(X) - CHARINDEX(X, ' ')))))
ELSE 'X'
END
FROM MyTable
Try this:
CASE
WHEN ISNUMERIC (Mid(Trim(X), 1)) THEN
UCASE(TRIM(MID(X, (CHARINDEX (X, " ", 1) +1)))))
ELSE
X
END