2013-02-13 54 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