SQL - Calculate two fields in one Case - mysql

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.

Related

Add a digit to the end of an SQL result

I want the result to add number in the end in specific cases.
Like: if result is between 1 and 50, add 1.
If result is between 51 and 99, add 2 to the end.
If result is between 100 and 200, add 3 to the end.
Like:
Result = 25, do it 251.
Result 67, do it 672.
Result is 150, do it 1503.
I have created a table but the cases don't seem to work. How would I add a digit in specific cases?
CREATE TABLE Numbers(
Num INT
);
INSERT Numbers VALUES('12');
INSERT Numbers VALUES('112');
INSERT Numbers VALUES('12');
INSERT Numbers VALUES('122');
INSERT Numbers VALUES('1');
INSERT Numbers VALUES('2');
INSERT Numbers VALUES('12345678');
INSERT Numbers VALUES('12345');
SELECT * FROM Numbers;
SELECT RIGHT('15'+ CONVERT(VARCHAR,Num),6) AS NUM FROM Numbers;
SELECT LEFT(REPLICATE('0', 10) + CONVERT(VARCHAR, Num), 6) AS NUM FROM Numbers;
SELECT RIGHT('0' + CAST(Num AS VARCHAR(2)), 2) FROM Numbers
SELECT
CASE
WHEN Num BETWEEN 1 AND 99
THEN LEFT ('00' + CAST(Num AS VARCHAR(2)), 2)
ELSE
CAST(Num AS VARCHAR(10))
END
FROM Numbers
Since you're already using varchar on these values, I'd use concat - which simply mergs strings together. In this case you simply select what you want to merge, with what. Documentation on Concat() here.
Fiddle: https://www.db-fiddle.com/f/at2fqinuEao3b8coRSydTD/1
SELECT
CASE WHEN Num BETWEEN 1 AND 50
THEN concat(Num, '1')
WHEN Num BETWEEN 51 AND 99
THEN concat(Num, '2')
WHEN Num BETWEEN 100 AND 199
THEN concat(Num, '3')
ELSE Num END AS Num
FROM Numbers
In the examples of your 25,67 and 150 - this is the result:
Num
251
672
1503
You're working with numbers, so you can do Num*10 + 1 etc. Like this. fiddle
SELECT CASE WHEN Num BETWEEN 1 AND 50 THEN Num*10 + 1
WHEN Num BETWEEN 51 AND 99 THEN Num*10 + 2
WHEN Num BETWEEN 100 AND 199 THEN Num*10 + 3
ELSE Num END AS Num
FROM Numbers
That seems like it might be easier than string-casting and concatenating.
But you could do this if you really want strings. fiddle.
SELECT CASE WHEN Num BETWEEN 1 AND 50 THEN CONCAT(Num, '1')
WHEN Num BETWEEN 51 AND 99 THEN CONCAT(Num, '2')
WHEN Num BETWEEN 100 AND 199 THEN CONCAT(Num, '3')
ELSE CONVERT(Num, CHAR) END AS Num
FROM Numbers

from and to ranges in mysql

Currently I have this kind of query
SELECT
part_number,
part_name,
attenuation_low_end,
attenuation_high_end,
optimum_fermentation_temp_f_low,
optimum_fermentation_temp_f_high
FROM
yeast_module
WHERE
category = 3
AND
(
( `attenuation_low_end` > '31' OR `attenuation_low_end` = '31' )
AND
( `attenuation_high_end` < '40' OR `attenuation_high_end` = '40' )
)
Where I'm trying to get the records with the range of low to high end from 31 and maximum of 40
But it returns me something like this
As you can notice it seems doesn't return the data between 31 to 40
Am I doing this right?
UPDATE
I'm expecting no return since, there's no data between 31-40
You're comparing strings, which performs lexicographic comparisons rather than numeric comparisons. You need to convert to numbers. Adding 0 to a numeric string is a simple way to convert it to a number.
WHERE 0+attenuation_low_end >= 31 AND 0+attenuation_high_end <= 40
If you want ranges contained in the 31-40 range:
where attenuation_low_end >= 31 and attenuation_high_end <= 40
If you want ranges that overlap the 31-40 range:
where attenuation_low_end <= 40 and attenuation_high_end >= 31
If your data is of a string datatype, then you need to convert the values to integers so they can be compared as such.
Containment:
where attenuation_low_end + 0 >= 31 and attenuation_high_end + 0 <= 40
Overlap:
where attenuation_low_end + 0 <= 40 and attenuation_high_end + 0 >= 31

SQL statement equivalent to ternary operator

I would like to create a statement that is equivalent to (x - y == 0) ? return 0 : return 100 in MySQL. Something that might look like this:
SELECT id, [(integer_val - 10 == 0) ? 0 : 100] AS new_val FROM my_table
I want to compare an attribute in each row to a certain number, and if the difference between that number and the number in the row is 0, I want it to give me 0, otherwise, I want it to give me 100.
Example:
Applying this query on my_table (with 10 being the 'compared to' number):
id | integer_val
===================
1 10
2 10
3 3
4 9
Would return this:
id | new_val
===================
1 100
2 100
3 0
4 0
How can I do this?
Try this:
SELECT id, IF(integer_val = 10, 100, 0) AS new_val
FROM my_table;
OR
SELECT id, (CASE WHEN integer_val = 10 THEN 100 ELSE 0 END) AS new_val
FROM my_table;
Use case when statement:
select *, (case when integer_val = 10 then 100 else 0 end) as New_Val
from yourtable
Try using the IF function:
SELECT id, IF(integer_val - 10 = 0, 0, 100) AS new_val FROM my_table
(I stuck with your condition expression, but it can be simplified a bit since integer_value - 10 = 0 has exactly the same truth value as integer_value = 10.)
Note that the IF function is different from MySQL's IF statement used for stored programs.

Using Previous Record Function in Formula and then creating a Group Summary

I have been trying to figure this out and I am stumped. I am finding the difference in minutes between two times by using the following formula:
This formula is called {#CALCULATIONS}
if {Location.Name} = previous ({Location.Name}) then
( hour({DownloadData.Date}) * 60 + minute({DownloadData.Date}) )-
( hour(previous({DownloadData.Date})) * 60 + minute(previous({DownloadData.Date})) )
else
0
I then uses the answer it comes up with in a formula that determines if it meets a criteria. If it does I assign it a 1 if not I assign it a zero. Here are the 5 different formulas I have for time ranges.
{#1 to 30} if {#Calculations} in 1 to 30 then 1 else 0
{#31 to 45} if {#Calculations} in 31 to 45 then 1 else 0
{#46 to 60} if {#Calculations} in 46 to 60 then 1 else 0
{#61 to 90} if {#Calculations} in 61 to 90 then 1 else 0
{#Morethan90} if {#Calculations} >90 then 1 else 0
For a while I have been trying to figure out how to get it to total the 1s that meet the formula criteria in the group.
Any suggestions?

Update Table with Case stmt

This is a fun side project I have been working on. I added a column for weight (op1w to op12w) to go with the description (op_1 to op_12). Now I need to go back through and update all entries with the appropriate op1w to op12w depending on the op_1 to op_12. Here is a part of the drop_log table with those columns.
Here are the values to match and what to update the op1w to op12w with (this exists as a table called mechs in the db):
mech weight
AS7 100
AWS 80
BJ 45
BLR 85
CDA 40
CN9 50
COM 25
CPL 65
CTF 70
DISC 0
DRG 60
GRF 55
HBK 50
HGN 90
JM6 65
JR7 35
KTO 55
LCT 20
ON1 75
QKD 60
RVN 35
SDR 30
SHD 55
STK 85
TBT 50
TDR 65
VTR 80
WVR 55
Would you guys help me with the case statement for this? I believe it should start something like this:
UPDATE drop_log
SET
op1w =(case when some_case_condition then something else op1w end),
op2w =(case when some_case_condition then something else op2w end)
.
Thanks for your help.
edit: OK after reading the comments, here is my attept
UPDATE drop_log
SET op1w=
CASE
WHEN op_1 = 'AS7'
THEN op1w = 100
WHEN op_1 = 'AWS'
THEN op1w = 80
.
.
.
ELSE op_1
END
CASE
WHEN op_12 = etc
THEN op12w = etc
.
.
.
ELSE op_12
END
Ok now I see what you guys are saying.. No I don't want you to fill it our for me. This is going to go on and on for each column 1-12 and then each column gets its own case for each weight possibility. Is that all there is to it? I thought it would be more involved.
The general format of your query should be:
UPDATE drop_log
SET op1w = CASE
WHEN op_1 = 'AWS' THEN '1'
WHEN op_2 = 'ABC' THEN op1
ELSE '0'
END,
op2w = CASE
WHEN op_1 = 'HDP' THEN op_1
ELSE '0'
END