************** 已解决,但欢迎提供更好的答案!:) **************
我想从表 sc000 中获取 (sa003*sa004) 的总和,在此之前我必须检查其他表上是否存在项目。图片(某些表格具有相同的列格式):
我使用'IN':
... WHERE s.sa002 IN (select b.b005 from b000 b where year(b.b001) = 2013 and month(b.b001) = 1)
但这需要一个漫长的过程,所以我正在尝试使用“LEFT JOIN”:
SELECT s.sa002, Sum(s.SA003) AS qty, Sum(s.SA003*s.SA004) AS jumlah
FROM sc000 s LEFT JOIN
(select bs.b005 from b000 bs where year(bs.b001) = 2013 and month(bs.b001) = 1) b
on s.sa002 = b.b005 LEFT JOIN
(SELECT ps.p005 from p000 ps where year(ps.p001) = 2013 and month(ps.p001) = 1 ) p
on s.sa002 = p.p005 LEFT JOIN
(SELECT js.b005 from j000 js where year(js.b001) = 2013 and month(js.b001) = 1 ) j
on s.sa002 = j.b005 LEFT JOIN
(SELECT rs.b005 from Rev00 rs where year(rs.b001) = 2013 and month(rs.b001) = 1) rv
on s.sa002 = rv.b005 LEFt JOIN
(SELECT es.b005 FROM R000 es where year(es.b001) = 2013 and month(es.b001) = 1) re
on s.sa002 = re.b005
where year(s.sa005) = 2013 and month(s.sa005) = 1
and (b.b005 is not null or p.p005 is not null or rv.b005 is not null or j.b005 is not null or re.b005 is not null)
GROUP BY s.sa002
结果相乘。有什么帮助吗?:(
更新:我想要这样的结果(代码,来自 sc002 的数量,数量 * 价格):
^ 不检查其他表格。
然后,使用左连接,结果如下: