所以我想让产品在囚犯工作天数和特定任务的乘数系数之和。 我试过这段代码:
UPDATE prisoners
SET nr_days_equaled = (SELECT COALESCE (multiplication_coefficient*SUM(nr_days_worked),0)
FROM prisoners pr
INNER JOIN timesheets_prisoners tp ON pr.idprisoner= tp.idprisoner
INNER JOIN nomenclature_activities_prisoners nap ON pp.idactivity=nap.idactivity)
但是我收到以下错误:
列“nap.multiplication_coefficient”必须出现在GROUP BY子句中或用于聚合函数
我该怎么办?有什么想法吗?
答案 0 :(得分:0)
欢迎使用StackOverflow。
在select语句中使用任何聚合函数(SUM,COUNT,MIN MAX等)和任何其他字段时,必须选择GROUP By其他字段。所以在你的情况下,编译器想要:
UPDATE prisoners
SET nr_days_equaled =
(SELECT COALESCE (multiplication_coefficient*SUM(nr_days_worked),0)
FROM prisoners pr INNER JOIN timesheets_prisoners tp
ON pr.idprisoner= tp.idprisoner
INNER JOIN nomenclature_activities_prisoners nap
ON pp.idactivity=nap.idactivity GROUP BY multiplication_coefficient)
但是,如果没有看到表格的内部结构,我怀疑这不是你想要的。我怀疑你真的需要
SET nr_days_equaled =
(SELECT COALESCE (SUM(multiplication_coefficient*nr_days_worked),0)
FROM prisoners pr INNER JOIN timesheets_prisoners tp
ON pr.idprisoner= tp.idprisoner
INNER JOIN nomenclature_activities_prisoners nap
ON pp.idactivity=nap.idactivity)
不同之处在于此版本允许不同的multiplication_coefficients用于不同的工作,但仍然总和。我强调我不确定。请在发出UPDATE之前将其转换为SELECT语句进行测试。
修改
看过结构后,你需要的是:
UPDATE prisoners
SET nr_days_equaled = SubQ.sumdaysmult
FROM
(SELECT pr.idprisoner, COALESCE (ppnap.sumdaysmult, 0) AS sumdaysmult FROM
FROM prisoners pr LEFT JOIN (SELECT tp.idprisoner, SUM(tp.nr_days_worked
* nap.multiplication_coefficient) as sumdaysmult
FROM timesheets_prisoners tp
INNER JOIN nomenclature_activities_prisoners nap
ON tp.idactivity=nap.idactivity GROUP BY tp.idprisoner) tpnap
ON tpnap.idprisoner = pr.idprisoner) AS SubQ
WHERE SubQ.idprisoner = prisoners.idprisoner
答案 1 :(得分:0)
您应该使用nap.multiplication_coefficient
来使用group