SQL 使用其他组件计算空变量的数据库
SQL database calculation for null variable using other components
我希望通过在医疗保健计算器上使用预定义的权重来计算空变量,并且我希望输出是变量权重除以可用总权重的总和。正如您在下面看到的那样,这可能是“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
当值为 1 或 0 时,您不需要 CASE
表达式。条件的值为 1
为真时为 0
,否则为 0
。所以直接用condition就可以了
而且您不需要将案例嵌套在另一个案例中。
所有包含空格的列名都需要反引号。
UPDATE trial
SET `ABCD² Score for TIA` = (
Age >= 60
+ `BP systolic` >= 140
+ `BP diastolic >= 90`
+ (case `Clinical features of the TIA`
WHEN 'Unilateral weakness' then 2
WHEN 'Speech disturbance without weakness' THEN 1
ELSE 0
END)
+ (case when `Duration of symptoms` BETWEEN 10 AND 59 THEN 1
when `Duration of symptoms` >= 60 then 2
ELSE 0
END)
+ `History of diabetes` = 'Yes')
WHERE `ABCD² Score for TIA` IS NULL
您必须修复 CASE 表达式并使用 MySql 的功能将布尔表达式计算为 1 或 0:
update trial
set `ABCD² Score for TIA` = (
(Age >= 60) + (`BP systolic` >= 140) + (`BP diastolic` >= 90) +
case `Clinical features of the TIA`
when 'Unilateral weakness' then 2
when 'Speech disturbance without weakness' then 1
when 'Other symptoms' then 0
end +
case
when `Duration of symptoms` >= 60 then 2
when `Duration of symptoms` >= 10 then 1
when `Duration of symptoms` < 10 then 0
end +
(`History of diabetes` = 'Yes')
) / 8
where `ABCD² Score for TIA` is null
参见demo。
结果:
> Name | Age | BP systolic | BP diastolic | Clinical features of the TIA | Duration of symptoms | History of diabetes | ABCD² Score for TIA
> :------- | --: | ----------: | -----------: | :---------------------------------- | -------------------: | :------------------ | -------------------:
> Person A | 71 | 137 | 85 | Speech disturbance without weakness | 17 | Yes | 0.5
> Person B | 92 | 125 | 78 | Other symptoms | 43 | Yes | 0.375
> Person C | 27 | 130 | 90 | Other symptoms | 34 | No | 0.25
我希望通过在医疗保健计算器上使用预定义的权重来计算空变量,并且我希望输出是变量权重除以可用总权重的总和。正如您在下面看到的那样,这可能是“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
当值为 1 或 0 时,您不需要 CASE
表达式。条件的值为 1
为真时为 0
,否则为 0
。所以直接用condition就可以了
而且您不需要将案例嵌套在另一个案例中。
所有包含空格的列名都需要反引号。
UPDATE trial
SET `ABCD² Score for TIA` = (
Age >= 60
+ `BP systolic` >= 140
+ `BP diastolic >= 90`
+ (case `Clinical features of the TIA`
WHEN 'Unilateral weakness' then 2
WHEN 'Speech disturbance without weakness' THEN 1
ELSE 0
END)
+ (case when `Duration of symptoms` BETWEEN 10 AND 59 THEN 1
when `Duration of symptoms` >= 60 then 2
ELSE 0
END)
+ `History of diabetes` = 'Yes')
WHERE `ABCD² Score for TIA` IS NULL
您必须修复 CASE 表达式并使用 MySql 的功能将布尔表达式计算为 1 或 0:
update trial
set `ABCD² Score for TIA` = (
(Age >= 60) + (`BP systolic` >= 140) + (`BP diastolic` >= 90) +
case `Clinical features of the TIA`
when 'Unilateral weakness' then 2
when 'Speech disturbance without weakness' then 1
when 'Other symptoms' then 0
end +
case
when `Duration of symptoms` >= 60 then 2
when `Duration of symptoms` >= 10 then 1
when `Duration of symptoms` < 10 then 0
end +
(`History of diabetes` = 'Yes')
) / 8
where `ABCD² Score for TIA` is null
参见demo。
结果:
> Name | Age | BP systolic | BP diastolic | Clinical features of the TIA | Duration of symptoms | History of diabetes | ABCD² Score for TIA
> :------- | --: | ----------: | -----------: | :---------------------------------- | -------------------: | :------------------ | -------------------:
> Person A | 71 | 137 | 85 | Speech disturbance without weakness | 17 | Yes | 0.5
> Person B | 92 | 125 | 78 | Other symptoms | 43 | Yes | 0.375
> Person C | 27 | 130 | 90 | Other symptoms | 34 | No | 0.25