我有两个带数字和平均值的字段,但是当我尝试获取总量时,第一行总是为NULL。
INSERT INTO weight(
DATE,
WEIGHT1,
WEIGHT2,
AVERAGE_WEIGHT,
TOTAL
)
VALUES (
GetDate(),
convert(real, '518'),
convert(real, '510'),
convert(real, '514'),
(SELECT SUM(average_weight) FROM weight)
)
这个查询工作,但问题是一些sum函数。
WEIGHT1 WEIGHT2 AWERAGE_WEIGHT TOTAL
518 510 514 0
518 510 514 514
第一行(TOTAL)应为514秒行(TOTAL)应为1028 如何正确地获得正确的价值?
答案 0 :(得分:1)
这是因为您的选择在插入新值之前获得当前总数 - 试试这个:
INSERT INTO weight(
DATE,
WEIGHT1,
WEIGHT2,
AVERAGE_WEIGHT,
TOTAL
)
VALUES (
GetDate(),
convert(real, '518'),
convert(real, '510'),
convert(real, '514'),
(SELECT SUM(average_weight) + convert(real, '514') FROM weight)
)