**************已解決,但更好的答案是歡迎! :) **************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