0

************** 已解决,但欢迎提供更好的答案!:) **************

我想从表 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 的数量,数量 * 价格):

在此处输入图像描述

^ 不检查其他表格。

然后,使用左连接,结果如下:

在此处输入图像描述

4

2 回答 2

0

您的一个(或一些)联接没有以一对一的关系链接。最简单的检查方法是删除 group by 和 sum() 语句,并从 b、p、j、rv 和 re 表中包含一些列,如下所示:

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
于 2013-02-13T04:55:44.873 回答
0

感谢所有看到或试图帮助解决这个问题的人。:D

我已经找到了查询!

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

于 2013-02-13T09:59:35.607 回答