试图返回每个零件号下所有订单的总和。主要问题在于选择子查询。
SELECT i.customer_no AS CustNumber,
i.part_no AS PartNumber,
SUM(f.weight) AS ForecastWeight,
(SELECT SUM(o.ord_ord_wgt)
FROM ortord_rec o
WHERE (o.ord_comp_dt < 20191126 AND o.ord_comp_dt > 20190925)
GROUP BY o.ord_cus_part_no
) AS UsageWeight,
p.cpt_prog_mat
FROM fit_part_info i
INNER JOIN fit_part_forecast f ON f.part_info_id = i.id
INNER JOIN orrcpt_rec p ON i.part_no = p.cpt_cus_part_no
INNER JOIN ortord_rec o ON i.part_no = o.ord_cus_part_no
WHERE accounting_period IN ('201911', '201912', '202001', '202002') AND cpt_prog_mat = "Y"
GROUP BY i.part_no, o.ord_cus_part_no, i.customer_no, p.cpt_prog_mat
ORDER BY i.part_no;
运行此命令时,出现错误:
子查询未完全返回一行。
答案 0 :(得分:1)
大概,您打算建立一个相关子查询:
(SELECT SUM(o.ord_ord_wgt)
FROM ortord_rec o
WHERE o.ord_comp_dt < 20191126 AND
o.ord_comp_dt > 20190925 AND
o.ord_cus_part_no = i.part_no
) AS UsageWeight,