【问题标题】:how to compare two sql query values如何比较两个sql查询值
【发布时间】:2015-06-05 04:51:40
【问题描述】:

我有两个表 StockOutward 和产品 outward..我必须获取不等于 sum(qty) stockOutward 的 sum(qty)..

StockOutward

Id ProductId Qty Location Orderid 1 7 2 2 38 2 8 1 2 38 3 7 1 2 38

产品外向

Id ProductId Qty Location Orderid 1 7 12 2 38 2 8 1 2 38

我需要 stockoutward 的输出为 ProductId 7

我使用了以下查询

Select
    sum(qty) as Qty,ProductId 
from 
    StockOutward 
where 
    Orderid='38' 
group by 
    ProductId 

Union

Select 
    sum(qty) as Qty,
    ProductId 
from 
    ProductOutward 
where 
    Orderid='38' 
group by 
    ProductId

【问题讨论】:

    标签: sql-server sql-server-2008


    【解决方案1】:

    您可以使用JOIN 并过滤不等式:

    SELECT
        s.ProductId
    FROM (
        SELECT
            ProductId,
            SumQty = SUM(Qty)
        FROM StockOutward
        GROUP BY ProductId
    )s
    INNER JOIN (
        SELECT
            ProductId,
            SumQty = SUM(Qty)
        FROM ProductOutward
        GROUP BY ProductId
    )p
        ON p.ProductId = s.ProductId
        AND p.SumQty <> s.SumQty
    

    【讨论】:

    • 谢谢。我会测试并说@wewestthemenace
    【解决方案2】:
    Select sum(qty) as Qty,ProductId from StockOutward where Orderid='38' group by ProductId 
    except
    Select sum(qty) as Qty,ProductId from ProductOutward where Orderid='38' group by ProductId
    

    【讨论】:

      猜你喜欢
      • 2011-04-15
      • 1970-01-01
      • 2010-11-27
      • 1970-01-01
      • 2022-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多