gpt4 book ai didi

sql-server - 如何比较两个sql查询值

转载 作者:行者123 更新时间:2023-12-01 13:52:26 25 4
gpt4 key购买 nike

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

StockOutward

Id ProductId Qty Location Orderid1     7       2     2        382     8       1     2        383     7       1     2        38

ProductOutward

Id ProductId Qty Location Orderid1     7       12     2        382     8        1     2        38

I need the output from stockoutward as ProductId 7

I have used the below query

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

最佳答案

您可以使用 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

关于sql-server - 如何比较两个sql查询值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30658570/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com