**************已解决,但更好的答案是欢迎! :) **************SQL - 总计字段仅用于当前表加入5个表
我想从表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数量,数量*价格):
^没有检查其他表
,然后,用左手加入,结果是这样的:哟
您能否提供您的表格定义,样本数据和所需的输出。这太难以阅读了,我认为你缺少图表中的字段。建立一个小提琴(sqlfiddle.com),这将使我们更容易帮助你。 – sgeddes 2013-02-13 03:41:43
我正在更新我的问题,并会寻找(sqlfiddle.com)感谢sgeddes – Krofz 2013-02-13 03:53:57