DB2 - 查找不匹配 - 获取查询

时间:2013-09-18 11:01:52

标签: sql db2

我有两张桌子,

  1. 首先 - 个别股票 - 每个股票 - 每行 - 产品ID为FK,
  2. 秒 - 产品 - 每个产品 - 每行。有数量列。
  3. 当然,库存表中的库存数量等于产品表中的数量。 我想找到不匹配的地方。

    我写了一个如下查询,

    select a.stockid from stock a , product b where a.productid = b.productid and sum(a.quantity)<> b.quantity with ur.
    

    但它抛出一个错误。在适当的PLZ中使用函数。

    还有其他方法可以解决这个问题。

    同时还有任何方法可以打印b.quantitysum(a.quantity)以及a.stockid

1 个答案:

答案 0 :(得分:1)

我认为您需要以下查询:

select s.stockid, p.productid, p.quantity
from stock s join
     product p
     on p.productid = s.productid
group by p.productid, p.quantity 
having sum(s.quantity) <> p.quantity;

使用having子句比较汇总结果,而不是where子句。

相关问题