Is there any short form for writing MySQL CASE statements as in PHP switch? For example, if i have:
SELECT
CASE Type
WHEN 1 THEN "A"
WHEN 5 THEN "B"
WHEN 10 THEN "B"
WHEN 20 THEN "B"
WHEN 30 THEN "B"
WHEN 300 THEN "C"
ELSE "-"
END TypeDesc
In PHP syntax we can write it in brief:
switch ($Type) {
case 1: $TypeDesc = 'A'; break;
case 5:
case 10:
case 20:
case 30: $TypeDesc = 'B'; break;
case 300: $TypeDesc = 'C'; break;
default: $TypeDesc = '-'; }
I tried:
WHEN 5 OR 10 OR 20 OR 30 THEN "B"
and
WHEN IN(5, 10, 20, 30) THEN "B"
and these two:
WHEN 5
WHEN 10
WHEN 20
WHEN 30 THEN "B"
WHEN 5 THEN
WHEN 10 THEN
WHEN 20 THEN
WHEN 30 THEN "B"
But i get incorrect result or error!
Use
case when <condition>
instead of
case <variable> when <value>
like this
SELECT
CASE WHEN Type = 1 THEN "A"
WHEN Type < 300 THEN "B"
WHEN Type = 300 THEN "C"
ELSE "-"
END TypeDesc
Related
how can I create a function that determines_grade which accepts the grade between 0-100 and returns the letter grade based on the following scale?
. 90-100 A
. 80-89 B
. 70-79 C
. 60-69 D
. below 60 F
and call my function with each of the following argument
function call score
1 90
2 85
3 73
4 62
5 58
In Python, you can use the if/elif/else logic in your function, and since this logic evaluates from the "top-down", after checking if the input is valid, only a single criteria has to be checked at each step. For example:
def determine_grade(score):
if score < 0 or score > 100:
return "Score not between 0-100"
elif score >= 90:
return "A"
elif score >= 80:
return "B"
elif score >= 70:
return "C"
elif score >= 60:
return "D"
else:
return "F"
I have a sql query that calculates a field as follows:
Select
Case When b.xField > 10
then 20
when b.xField > 48
then 10
else 0
end as field1
from (Select CASE WHEN numberChosen < 15
THEN 10
WHEN numberChosen > 15
THEN 48
END as xField
From secondTable) B
What I need is, when field1 is 10, then do some other calculations to save on another field.
Example something like:
then 20 AND field2 = yData - 26
so when the case is on 20, then have another field equal to yData - 26.
Can that be achieved using Cases in Sql ? Have two fields calculated in a single case?
I want to calculate two fields in one Case
You Can't Do Thatâ„¢.
You can put two case clauses in your query like this:
Select
Case When xField > 10
then 20
when xField > 48
then 10
else 0
end as field1,
Case When xField > 10
then ydata - 26
else 0
end as field2
from myData
Or you can generate the extra field value in a wrapper query if it is really hard to compute field2
SELECT field1, CASE WHEN field1 > 10 THEN ydata -20 ELSE 0 END field2
FROM (
Select
ydata,
Case When xField > 10
then 20
when xField > 48
then 10
else 0
end as field1
from myData
) subquery
You can use the base logic that is deciding when field1 = 10 (ie. xField > 48) in the second case:
SELECT CASE WHEN xField > 48
THEN 10
WHEN xField > 10
THEN 20
ELSE 0
END as field1,
CASE WHEN xField > 48
THEN yData - 26
END as field2
FROM (SELECT CASE WHEN numberChosen < 15
THEN 10
WHEN numberChosen > 15
THEN 48
END as xField,
yData
FROM secondTable) B
I changed the order of your case because putting the > 10 condition before the > 48 condition will never let the > 48 be hit.
I am trying to change the background colour of a cell based on its contents. We are looking at grades, either 'A to U' or '9 to 1'. The cell needs to be highlighted if the grades are 2 lower than their target grade. We have it working if the cells are just numbers or just letters but not both.
Here is the Custom Code for the report:
Public Function gradeCheck(SubjectGrade AS String, TargetGrade AS String) AS String
Dim gradeValue = convertGradeToInt(SubjectGrade)
Dim targetValue = convertGradeToInt(TargetGrade)
If (targetValue - gradeValue) < -1
Return "Orange"
Else
Return "White"
End If
End Function
Public Function gradeCheckInt(SubjectGradeInt AS Integer, TargetGradeInt AS Integer) AS String
If (TargetGradeInt - SubjectGradeInt) >= 2
Return "Orange"
Else
Return "White"
End If
End Function
Public Function convertGradeToInt(grade AS String) AS Integer
Select Case grade
Case "A*"
return 1
Case "A"
return 2
Case "B"
return 3
Case "C"
return 4
Case "D"
return 5
Case "E"
return 6
Case "F"
return 7
Case "G"
return 8
Case "U"
return 9
End Select
End Function
Here is the code that works for numbers on their own or letters on their own:
Letters
= IIF(ISNOTHING(Fields!report_Grade.Value)
, "White"
, IIF(ISNOTHING(Fields!TargetGrade.Value)
, "White"
,code.gradeCheck(Fields!report_Grade.Value, Fields!TargetGrade.Value)
)
)
Numbers
= IIF(ISNOTHING(Fields!report_Grade.Value)
, "White"
, IIF(ISNOTHING(Fields!TargetGrade.Value)
, "White"
,code.gradeCheckInt(Fields!report_Grade.Value, Fields!TargetGrade.Value)
)
)
And here is what I feel should work, but is only highlighting the correct numbers, not letters:
= IIF(ISNOTHING(Fields!report_Grade.Value)
, "White"
, IIF(ISNOTHING(Fields!TargetGrade.Value)
, "White"
,IIF(IsNumeric(Fields!report_Grade.Value)
,code.gradeCheckInt(Fields!report_Grade.Value, Fields!TargetGrade.Value)
,code.gradeCheck(Fields!report_Grade.Value, Fields!TargetGrade.Value)
)
)
)
Any help would be greatly appreciated.
Thanks
It looks like your grades always come from the same column so this must be a string (even if it contains a number). If this is the case then I would modify your convertGradeToInt function by testing if the string is a value something like
Public Function convertGradeToInt(grade AS String) AS Integer
If Val(grade) > 0 then
return Val(grade)
ELSE
Select Case grade
Case "A*"
return 1
Case "A"
return 2
Case "B"
return 3
Case "C"
return 4
Case "D"
return 5
Case "E"
return 6
Case "F"
return 7
Case "G"
return 8
Case "U"
return 9
End Select
End If
End Function
Now, if the passed in value is numeric you just get the same value back, if it's a string it returns the matching grade (or zero if not match found).
Now your color expression can be simple
=IIF(Code.convertGradeToInt(Fields!TargetGrade.Value) - Code.convertGradeToInt(Fields!report_Grade.Value) >= 2 , "Orange", Nothing)
You should not need any other code, just the one function and one expression for the colour.
I may have overlooked something as it's not tested but hopefully it will give you enough to solve the problem.
Thanks to Alan above I was able to make it work. Here is the final custom code I used:
Here is the final custom code I used:
Public Function gradeCheck(SubjectGrade AS Integer, TargetGrade AS Integer) AS String
If TargetGrade - SubjectGrade >= 2
Return "Orange"
Else
Return "White"
End If
End Function
Public Function convertGradeToInt(grade AS String) AS Integer
If Val(grade) > 0 then
return Val(grade)
ELSE
Select Case grade
Case "A*"
return 9
Case "A"
return 8
Case "B"
return 7
Case "C"
return 6
Case "D"
return 5
Case "E"
return 4
Case "F"
return 3
Case "G"
return 2
Case "U"
return 1
End Select
End If
End Function
And here is the colour expression:
= IIF(ISNOTHING(Fields!report_Grade.Value)
, "White"
, IIF(ISNOTHING(Fields!TargetGrade.Value)
, "White"
, code.gradeCheck(Code.convertGradeToInt(Fields!report_Grade.Value), Code.convertGradeToInt(Fields!TargetGrade.Value))
)
)
The question I try to solve:
Return the value for "prefix" following these conditions:
If model_hyouji is a number and ranging from 0 to 9, set prefix = 0
If model_hyouji is a letter and ranging from a to z or A to Z, set prefix = 1
In all other cases, set prefix = 2
I used this SQL:
SELECT model_code,
model_hyouji,
model_name_prefix,
model_kana_prefix,
model_count,
case prefix
when 'a' then 1
when 'b' then 2
else 3
end
FROM webikept.mst_model_v2
where model_maker_code = 1
and model_displacement between 51 and 125
and (SELECT substring(model_hyouji,1,1) as prefix
FROM webikept.mst_model_v2 )
and I get this error:
Unknown column 'prefix' in 'field list'
assuming you column model_hyouji is varchar(1) (otherwise use substr(model_hyouji,1) )
SELECT model_code,
model_hyouji,
model_name_prefix,
model_kana_prefix,
model_count,
case
when model_hyouji between '0' and '9' then 0
when model_hyouji between 'a' and 'z' then 1
when model_hyouji between 'A' and 'Z' then 1
else 2
end prefix
FROM webikept.mst_model_v2
where model_maker_code = 1
and model_displacement between 51 and 125
and (SELECT substring(model_hyouji,1,1) as prefix
FROM webikept.mst_model_v2 )
or you can use regexp
SELECT model_code,
model_hyouji,
model_name_prefix,
model_kana_prefix,
model_count,
case
when model_hyouji REGEXP '^[0-9]{1}$' then 0
when model_hyouji REGEXP '^[A-Za-z]$' then 1
else 2
end prefix
FROM webikept.mst_model_v2
where model_maker_code = 1
and model_displacement between 51 and 125
and (SELECT substring(model_hyouji,1,1) as prefix
FROM webikept.mst_model_v2 )
SELECT
model_code,model_hyouji,model_name_prefix,model_kana_prefix,model_count,
case
when prefix = 'a' then 1 when 'b' then 2 else 3 end
You need CASE WHEN prefix =
But you also dont specify which prefix column you want to use hence the error on unknown column
E.G
SELECT
model_code,model_hyouji,model_name_prefix,model_kana_prefix,model_count,
case
WHEN model_kana_prefix = 'a' then 1
WHEN model_kana_prefix = 'b' then 2
else 3 end
There are 2 cases given in the question and on that basis we have to answer.
Cases:
if((NOT(value>=1) OR NOT(value<=10))
if((NOT(value>=1) AND NOT(value<=10))
Now the questions are:
which case you are going to use if the given value either is 1 or 10 ?
which case you are going to use if the given value must be 1 or 10 ?
the problem is whether I takes 1 or 10 I am getting same answer in both the cases. That is if(0) and thus if statement is false in both the cases.?
(NOT(value>=1) OR NOT(value<=10)) = (value < 1) OR (value > 10)
This case is true for [-Infinity ... 0] or [11 ... + Infinity]
Is false for 1 or 10
((NOT(value>=1) AND NOT(value<=10)) = (value < 1) AND (value > 10)
This case is always false, as no number can be smaller than 1 and bigger than 10 the same time.