2013-02-13 36 views
0

**************已解決,但更好的答案是歡迎! :) **************SQL - 總計字段僅用於當前表加入5個表

我想從表sc000中獲得(sa003 * sa004)的總和,在此之前,我必須檢查其他表是否存在其他表。圖片(有些表有相同的列格式):

enter image description here

我使用 '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數量,數量*價格):

enter image description here

^沒有檢查其他表

,然後,用左手加入,結果是這樣的:喲

enter image description here

+0

您能否提供您的表格定義,樣本數據和所需的輸出。這太難以閱讀了,我認爲你缺少圖表中的字段。建立一個小提琴(sqlfiddle.com),這將使我們更容易幫助你。 – sgeddes 2013-02-13 03:41:43

+0

我正在更新我的問題,並會尋找(sqlfiddle.com)感謝sgeddes – Krofz 2013-02-13 03:53:57

回答

0

感謝大家誰看到或試圖幫助解決這個問題。 :D

我已經找到查詢!

我正在使用UNION加入5個表格,並得到了我想要的答案。

0

一個(或一些)你的加入並不是鏈接在一對一的關係中。最簡單的檢查方法是通過和SUM()語句,刪除組,包括從B,P,J,RV一些列和重新桌,就像這樣:

SELECT s.sa002, s.SA003 AS qty, s.SA003*s.SA004 AS jumlah, g.gName as gNames, 
p.p005, j.b005, rv.b005, re.b005 
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) 
ORDER BY s.sa002 

,或者甚至更好,

SELECT s.sa002, count(*) 
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 
HAVING count(*) > 1 
+0

感謝您的幫助Cha,但這不是我想要的答案:) – Krofz 2013-02-13 09:54:30