How To Calculates Different Values Based On Different Variables In MySQL?

How To Calculates Different Values Based On Different Variables In MySQL?


Creating Data Tables in MySQL: A Step-by-Step Guide


If you followed my previous post, you can manually create a Data Table. If you copy and paste the code below into your MySQL window and then run it with Ctrl+Shift+Enter, you will see the below data table.

CREATE DATABASE test_data;
CREATE TABLE test_data.crop_yield (
    ID INT AUTO_INCREMENT PRIMARY KEY,
    Reps INT,
    Variable VARCHAR(15),
    Yield DECIMAL(5, 2)
);

INSERT INTO test_data.crop_yield (Reps, Variable, Yield) VALUES
    (1, 'Control', 116.08),
    (2, 'Control', 132.85),
    (3, 'Control', 115.98);

INSERT INTO test_data.crop_yield (Reps, Variable, Yield) VALUES
    (1, 'Treatment', 131.37),
    (2, 'Treatment', 108.89),
    (3, 'Treatment', 100.35);
    
SELECT * FROM test_data.crop_yield;

How to calculate new values?

Now, I’d like to calculate new values. For example, I’ve discovered that the yield data was overestimated, and I want to subtract 20 from each value.

IDRepsVariableYieldYield_corrected
11Control116.0896.08
22Control132.85112.85
33Control115.9895.98
41Treatment131.37111.37
52Treatment108.8988.89
63Treatment100.3580.35

SELECT *, (Yield-20) AS Yield_corrected FROM test_data.crop_yield

How to calculate new values according to different variables?

Now, let’s take another step. I discovered that the yield data was overestimated in the Control group but underestimated in the Treatment group. I’ll subtract 20 in the Control group and add 10 in the Treatment group.

IDRepsVariableYieldYield_corrected
11Control116.0896.08
22Control132.85112.85
33Control115.9895.98
41Treatment131.37141.37
52Treatment108.89118.89
63Treatment100.35110.35

First, I’ll delete the ‘Yield_corrected’ column I created,

ALTER TABLE test_data.crop_yield
DROP COLUMN Yield_corrected;

and then I’ll create a new ‘Yield_corrected’ column.

ALTER TABLE test_data.crop_yield 
ADD COLUMN Yield_corrected DECIMAL(10, 2);

SELECT * FROM test_data.crop_yield;

Then, I’ll recalculate the yield.

UPDATE test_data.crop_yield
SET Yield_corrected = CASE
    WHEN Variable='Control' THEN Yield-20
    WHEN Variable='Treatment' THEN Yield+10
    ELSE Yield
END;

SELECT * FROM test_data.crop_yield;

Additionally, I’ve determined that moisture content is significant, and I’ve adjusted it to 15% to calculate the final corrected yield.

IDRepsVariableYieldYield_correctedYield_moisture
11Control116.0896.0881.67
22Control132.85112.8595.92
33Control115.9895.9881.58
41Treatment131.37141.37120.16
52Treatment108.89118.89101.06
63Treatment100.35110.3593.80

So, I’ll create a new column named ‘Yield_moisture’ and adjust the yield for a 15% moisture.

ALTER TABLE test_data.crop_yield 
ADD COLUMN Yield_moisture DECIMAL(10, 2);

UPDATE test_data.crop_yield
SET Yield_moisture= CASE
    WHEN Variable='Control' THEN Yield_corrected-Yield_corrected*0.15
    WHEN Variable='Treatment' THEN Yield_corrected-Yield_corrected*0.15
    ELSE Yield_corrected
END;

SELECT * FROM test_data.crop_yield;

#The full code: https://github.com/agronomy4future/SQL/blob/main/new_column_calculcated 

CREATE DATABASE test_data;
CREATE TABLE test_data.crop_yield (
    ID INT AUTO_INCREMENT PRIMARY KEY,
    Reps INT,
    Variable VARCHAR(15),
    Yield DECIMAL(5, 2)
);

INSERT INTO test_data.crop_yield (Reps, Variable, Yield) VALUES
    (1, 'Control', 116.08),
    (2, 'Control', 132.85),
    (3, 'Control', 115.98);

INSERT INTO test_data.crop_yield (Reps, Variable, Yield) VALUES
    (1, 'Treatment', 131.37),
    (2, 'Treatment', 108.89),
    (3, 'Treatment', 100.35);
    
ALTER TABLE test_data.crop_yield 
ADD COLUMN Yield_corrected DECIMAL(10, 2);

UPDATE test_data.crop_yield
SET Yield_corrected = CASE
    WHEN Variable='Control' THEN Yield-20
    WHEN Variable='Treatment' THEN Yield+10
    ELSE Yield
END;

ALTER TABLE test_data.crop_yield 
ADD COLUMN Yield_moisture DECIMAL(10, 2);

UPDATE test_data.crop_yield
SET Yield_moisture= CASE
    WHEN Variable='Control' THEN Yield_corrected-Yield_corrected*0.15
    WHEN Variable='Treatment' THEN Yield_corrected-Yield_corrected*0.15
    ELSE Yield_corrected
END;

SELECT * FROM test_data.crop_yield;

Leave a Reply

If you include a website address in the comment section, I cannot see your comment as it will be automatically deleted and will not be posted. Please refrain from including website addresses.