sql - 使用两个 SUM 对错误进行故障排除
问题描述
我有一个表格,它将用于供应商记分卡,有 11 个不同的字段可以分配 1-5 的值。允许空值。
我需要编写一个查询来计算每行填写的字段的平均值。换句话说,我可能在一行中将 TOTAL 除以 11,在另一行中将 TOTAL 除以 5。
我正在处理这个查询:
select
cf$_vendor_no,
cf$_party,
cf$_environmental,
cf$_inspections,
cf$_invoice_process,
cf$_ncr,
cf$_on_time_delivery,
cf$_qms,
cf$_safety,
cf$_schedule,
cf$_scope_of_work,
cf$_turn_times,
sum(nvl(cf$_environmental,0)
+nvl(cf$_inspections,0)
+nvl(cf$_invoice_process,0)
+nvl(cf$_ncr,0)
+nvl(cf$_on_time_delivery,0)
+nvl(cf$_qms,0)
+nvl(cf$_safety,0)
+nvl(cf$_schedule,0)
+nvl(cf$_scope_of_work,0)
+nvl(cf$_turn_times,0))
/
sum(
case when cf$_environmental is not null then 1 else 0 end +
case when cf$_inspections is not null then 1 else 0 end +
case when cf$_invoice_process is not null then 1 else 0 end +
case when cf$_ncr is not null then 1 else 0 end +
case when cf$_on_time_delivery is not null then 1 else 0 end +
case when cf$_qms is not null then 1 else 0 end +
case when cf$_safety is not null then 1 else 0 end +
case when cf$_schedule is not null then 1 else 0 end +
case when cf$_scope_of_work is not null then 1 else 0 end +
case when cf$_turn_times is not null then 1 else 0 end) --as "average"
from supplier_scorecard_clv
group by cf$_vendor_no, cf$_party, cf$_environmental, cf$_inspections, cf$_invoice_process, cf$_ncr, cf$_on_time_delivery, cf$_qms, cf$_safety, cf$_schedule, cf$_scope_of_work, cf$_turn_times
而且,它几乎可以工作。
我的代码中的第一个 SUM 将添加每一行中的值来给我一个总数。第一行 FARW002 总共得到 25 个,第二行得到 6 个,第三行得到 12 个。
我的代码中的第二个 SUM 也可以。我的第一排 FARW002 计数为 6,第二排计数为 2,第三排计数为 3。
但是,当我尝试将这些组合起来时,就像在上面的代码片段中一样,我收到“ORA-00923:FROM 关键字未在预期位置找到”错误,我不知道为什么。
解决方案
以下是问题的最终结果:
+nvl(cf$_turn_times,0))
/
sum(
当我将代码更改为此时-实际上我只是在四处闲逛-它起作用了:
+nvl(cf$_turn_times,0))/sum(
因此,将 / 和 SUM 与查询的其余部分分开(我这样做只是为了使代码对我来说更具可读性)导致了问题。
推荐阅读
- mysql - mySQL 按关键字选择最低级别的类别
- delphi-7 - 使用Delphi 7获取HTML,最后得到一个烦人的零
- xml - 代码未按预期写入子目录并且内存使用率很高
- asp.net - 小计显示时隐藏网格视图复选框
- linux - 使用 find 递归地将所有 .jpg 文件添加到它们自己的 html 元素中
- javascript - 带有 PopState 和 AJAX 的 HTML 5 History API 不断重新加载页面
- python - 如何使用另一个 numpy 数组修改 pandas 数据框中的所有值
- laravel - 如何使用另一个对象的 ID 获取一个对象?
- c++ - 这个解决方案背后的直觉
- json - JSON 行 Mime 类型