这是我的表
View.NavigatorButtons.ClickButton(NBDI_EDIT);
这是我的查询
CREATE TABLE `GRADES_Q1` (
`Date_Modified` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`LRN` BIGINT(12) NOT NULL DEFAULT '0',
`Last_Name` VARCHAR(50) NOT NULL,
`First_Name` VARCHAR(50) NOT NULL,
`Level` VARCHAR(3) NOT NULL,
`Mother_Tongue` VARCHAR(50) NULL DEFAULT NULL,
`Filipino` VARCHAR(50) NULL DEFAULT NULL,
`English` VARCHAR(50) NULL DEFAULT NULL,
`Science` VARCHAR(50) NULL DEFAULT NULL,
`Math` VARCHAR(50) NULL DEFAULT NULL,
`AP` VARCHAR(50) NULL DEFAULT NULL,
`MAPEH` VARCHAR(50) NULL DEFAULT NULL,
`Music` VARCHAR(50) NULL DEFAULT NULL,
`Arts` VARCHAR(50) NULL DEFAULT NULL,
`PE` VARCHAR(50) NULL DEFAULT NULL,
`Health` VARCHAR(50) NULL DEFAULT NULL,
`ESP` VARCHAR(50) NULL DEFAULT NULL,
`Computer` VARCHAR(50) NULL DEFAULT NULL,
`TLE` VARCHAR(50) NULL DEFAULT NULL,
`CAT` VARCHAR(50) NULL DEFAULT NULL,
`EPP` VARCHAR(50) NULL DEFAULT NULL,
`GEN_AVE` VARCHAR(50) NULL DEFAULT NULL,
PRIMARY KEY (`LRN`)
)COLLATE='latin1_swedish_ci'
ENGINE=InnoDB;
当我运行查询时,我想计算(总成绩总和)/(16位空列数)并将其设置为GEN_AVE或一般平均值。
例如,(我希望这样)
UPDATE GRADES_Q1
SET GEN_AVE = (SELECT sum(COALESCE(Mother_Tongue,0))
+ COALESCE(Filipino,0)
+ COALESCE(English,0)
+ COALESCE(Science,0)
+ COALESCE(Math,0)
+ COALESCE(AP,0)
+ COALESCE(MAPEH,0)
+ COALESCE(ESP,0)
+ COALESCE(Computer,0)
+ COALESCE(TLE,0)
+ COALESCE(CAT,0)
+ COALESCE(EPP,0)
/ (12 - (select sum(case when ENGLISH is null then 1 else 0 end
+ case when Math is null then 1 else 0 end
+ case when ESP is null then 1 else 0 end
+ case when Mother_Tongue is null then 1 else 0 end
+ case when Computer is null then 1 else 0 end
+ case when MAPEH is null then 1 else 0 end
+ case when Science is null then 1 else 0 end
+ case when AP is null then 1 else 0 end
+ case when EPP is null then 1 else 0 end
+ case when TLE is null then 1 else 0 end
+ case when CAT is null then 1 else 0 end
+ case when AP is null then 1 else 0 end)
as grandtotals)))
(计算为等级总数/非空列数。
我的问题,它给了我GEN_AVE的奇怪结果,但没有错误(是的!)。 请帮忙。感谢。
答案 0 :(得分:0)
你也许可以这样轻松地做到这一点:
UPDATE GRADES_Q1
SET GEN_AVE =
(
COALESCE(Mother_Tongue,0))
+ COALESCE(Filipino,0)
+ COALESCE(English,0)
+ COALESCE(Science,0)
+ COALESCE(Math,0)
+ COALESCE(AP,0)
+ COALESCE(MAPEH,0)
+ COALESCE(ESP,0)
+ COALESCE(Computer,0)
+ COALESCE(TLE,0)
+ COALESCE(CAT,0)
+ COALESCE(EPP,0)
)
/
(
12
- case when ENGLISH is null then 1 else 0 end
- case when Math is null then 1 else 0 end
- case when ESP is null then 1 else 0 end
- case when Mother_Tongue is null then 1 else 0 end
- case when Computer is null then 1 else 0 end
- case when MAPEH is null then 1 else 0 end
- case when Science is null then 1 else 0 end
- case when AP is null then 1 else 0 end
- case when EPP is null then 1 else 0 end
- case when TLE is null then 1 else 0 end
- case when CAT is null then 1 else 0 end
- case when AP is null then 1 else 0 end
)