I’m looking to calculate the null variable by using predifined weights on a healthcare calculator and I want the output to be a sum of the variables weights over the total available weights. As you can see below this would hopefully be "sum/8".
CREATE TABLE `trial`.`trial` ( `Name` TEXT NULL , `Age` INT NULL , `BP systolic` INT NULL , `BP diastolic` INT NULL ,`Clinical features of the TIA` TEXT NULL , `Duration of symptoms` INT NULL , `History of diabetes` TEXT NULL , `ABCD² Score for TIA` FLOAT NULL ) ENGINE = InnoDB;
INSERT INTO `trial` (`Name`, `Age`, `BP systolic`, `BP diastolic`, `Clinical features of the TIA`, `Duration of symptoms`, `History of diabetes`, `ABCD² Score for TIA`) VALUES ('Person A', '71', '137', '85', 'Speech disturbance without weakness', '17', 'Yes', NULL);
INSERT INTO `trial` (`Name`, `Age`, `BP systolic`, `BP diastolic`, `Clinical features of the TIA`, `Duration of symptoms`, `History of diabetes`, `ABCD² Score for TIA`) VALUES ('Person B', '92', '125', '78', 'Other symptoms', '43', 'Yes', NULL);
INSERT INTO `trial` (`Name`, `Age`, `BP systolic`, `BP diastolic`, `Clinical features of the TIA`, `Duration of symptoms`, `History of diabetes`, `ABCD² Score for TIA`) VALUES ('Person C', '27', '130', '90', 'Other symptoms', '34', 'No', NULL);
update trial
set ABCD² Score for TIA = case when (
case when Age = >=60 then 1 else 0 end
+ case when BP systolic = >= 140 then 1 else 0 end
+ case when BP diastolic = >=90 then 1 else 0 end
+ case when Clinical features of the TIA = 'Unilateral weakness' then 2 end
+ case when Clinical features of the TIA = 'Speech disturbance without weakness' then 1 end
+ case when Clinical features of the TIA = 'Other symptoms' then 0 end
+ case when Duration of symptoms = <10 then 0 end
+ case when Duration of symptoms = 10-59 then 1 end
+ case when Duration of symptoms = >= 60 then 2 end
+ case when History of diabetes = 'Yes' then 1 else 0 end
) sum/8
where ABCD² Score for TIA is null
2
Answers
Yuo don’t need
CASE
expressions when the values are either 1 or 0. The value of a condition is1
when it’s true,0
otherwise. So just use the condition directly.And you don’t need the cases to be nested inside another case.
You need backticks around all the column names that contain spaces.
You must fix your CASE expressions and use MySql’s feature to evaluate boolean expressions as 1 or 0:
See the demo.
Results: