我有一个查询来获取有缺陷的零件,另外我需要在一个时间跨度内计算所有生产零件的百分比值。
我是一个SQL菜鸟,我得不到解决方案。我有这两个工作的查询,我想将它们结合起来计算生产件数量的百分比。
我在查询中尝试使用简单的子选择。但是他只计算包含缺陷部分的班次的数量。我不知道整个时间段的总和。所以我想把这两个问题结合起来。
在此之后我尝试加入和联合,但我只收到错误消息。
我缩短并重新显示所显示的查询以使它们更容易理解。
(Oracle 11gR2)
查询以获取部件
SELECT Production.LineName, Names.DescDefect, Sum(Errors.Rework)
FROM Production, Batchdata, Errors, Names, ShiftData, Shifts
WHERE ...=... and ShiftData.Date=...
GROUP Production.Line, Names.DescDefect
HAVING (Production.LineNumber Like '700_')
ORDER BY ...
结果linke this:
LineName DescDefect Sum(Errors.Rework)
Line1 Defect1 10
Line1 Defect3 2
Line3 Defect12 5
Line3 Defect53 5
查询零件数量:
SELECT Production.LineName, Sum(Batchdata.Produced)
FROM Production, Batchdata
WHERE ...=... AND ((Production.LineNumber Like '700_') AND (ShiftData.Date=...))
GROUP BY Production.LineName
结果:
LineName Sum(Batchdata.Produced)
Line1 500
Line3 700
我想要的是什么:
LineName DescDefect Sum(Errors.Rework) percent
Line1 Defect1 10 2
Line1 Defect3 2 0,4
Line3 Defect12 5 0,7
Line3 Defect53 5 0,7
答案 0 :(得分:0)
您可以使用select
中的子查询从视图中获取部分计数(with clause
)
WITH batchd AS
(SELECT Production.LineName LineName,
Sum(Batchdata.Produced) no_of_parts
FROM Production,
Batchdata WHERE ...=...
AND ((Production.LineNumber LIKE '700_')
AND (ShiftData.Date=...))
GROUP BY Production.LineName )
SELECT Production.LineName,
Names.DescDefect,
Sum(Errors.Rework),
(Sum(Errors.Rework) /
(SELECT no_of_parts
FROM batchd b
WHERE b.LineName = Production.LineName)) * 100 percent
FROM Production,
Batchdata,
Errors,
NAMES,
ShiftData,
Shifts WHERE ...=...
AND ShiftData.Date=...
GROUP Production.Line,
Names.DescDefect
HAVING (Production.LineNumber LIKE '700_')
ORDER BY ...