Sql server 组合具有相同id的两条记录的sql查询
我创建了一个查询来计算采购订单数量和销售订单数量。问题是,对于SO数量,对于同一产品,我得到两行不同SO数量的数据。我想做的是添加相同产品的SO数量,并在数据库中显示为一行。请提供建议 以下是我写的问题:Sql server 组合具有相同id的两条记录的sql查询,sql-server,Sql Server,我创建了一个查询来计算采购订单数量和销售订单数量。问题是,对于SO数量,对于同一产品,我得到两行不同SO数量的数据。我想做的是添加相同产品的SO数量,并在数据库中显示为一行。请提供建议 以下是我写的问题: SELECT DISTINCT products.ProductCode, products.ProductName, pe.stockreorderqty, pe.stocklowqtyalarm
SELECT DISTINCT products.ProductCode,
products.ProductName,
pe.stockreorderqty,
pe.stocklowqtyalarm,
products.StockStatus,
pe.lastpo_qty,
pe.lastpo_date,
pe.vendor_price,
Sum(PO_Items.POI_Quantity) - Sum(PO_Items.POI_QtyReceived) AS OutstandingPOQty,
orderdetails.Quantity as outstandingsoqty
FROM Products INNER JOIN Products_Extended pe WITH (NOLOCK) ON products.ProductID = pe.ProductID
LEFT JOIN PO_Items ON products.ProductCode = PO_Items.POI_ProductCode
LEFT JOIN orderdetails
INNER JOIN orders ON orderdetails.OrderID = orders.OrderID ON orderdetails.productcode = products.productcode
AND orders.OrderStatus <>'Cancelled'
AND orders.OrderStatus <>'Shipped'
AND orders.OrderStatus <>'Returned'
GROUP BY products.ProductCode, products.ProductName, pe.stockreorderqty,
pe.stocklowqtyalarm, products.StockStatus, pe.lastpo_qty,
pe.lastpo_date, pe.vendor_price, orderdetails.Quantity
在上述输出中,产品代码相同,但未付数量不同。我想添加outstandingsoqty并将两行显示为一行。
对于相同的产品代码,它们的未付款数量不同,对于不同的未结销售订单也是如此
您可以通过以下链接检查图像的输出:
有一种简单的方法可以做到这一点。不要在需要较大结果集的地方使用GROUPBY,这会使查询速度变慢,而应使用sub查询 这样做:
SELECT DISTINCT [OTHER Fields],
(SELECT Sum(PO_Items.POI_Quantity) - Sum(PO_Items.POI_QtyReceived) FROM PO_Items WHERE PO_Items.POI_ProductCode = products.ProductCode) as OutstandingPOQty,
(SELECT Sum(det1.Quantity) FROM orderdetails AS det1 WHERE det1.productcode =products.productcode and det1.OrderID=orders.OrderID) as outstandingsoqty
FROM Products
INNER JOIN Products_Extended pe WITH (NOLOCK) ON products.ProductID = pe.ProductID
LEFT JOIN orderdetails INNER JOIN orders ON orderdetails.OrderID = orders.OrderID ON orderdetails.productcode = products.productcode
and orders.OrderStatus <>'Cancelled' and orders.OrderStatus <>'Shipped' and orders.OrderStatus <>'Returned'
会很快的。希望能有所帮助。如果您格式化sql、输出并给出所需输出的示例,使用GROUP BY并对数量进行求和,您可能会得到更好的响应。如果我对未付款数量求和,那么对于相同的产品代码,我会得到两行不同的结果。只有未付款数量中的数字会更改。求和orderdetails.Quantity,并将其从组中删除。当我求和时,我得到了错误的数量。请告知。我希望未付款数量是orders.OrderStatus“已取消”和orders.OrderStatus“已发货”以及orders.OrderStatus“已退回”的数量之和。请告知。现在,它给了我所有订单数量的总和add det1.OrderID=orders.OrderID在outstandingsoqty子查询中。根据修改后的查询将得到的结果告诉我,以便我可以更正。例如,对于产品代码10507,我得到两条未结清的记录数量1为第一条记录,数量2为第二条记录
SELECT DISTINCT [OTHER Fields],
(SELECT Sum(PO_Items.POI_Quantity) - Sum(PO_Items.POI_QtyReceived) FROM PO_Items WHERE PO_Items.POI_ProductCode = products.ProductCode) as OutstandingPOQty,
(SELECT Sum(det1.Quantity) FROM orderdetails AS det1 WHERE det1.productcode =products.productcode and det1.OrderID=orders.OrderID) as outstandingsoqty
FROM Products
INNER JOIN Products_Extended pe WITH (NOLOCK) ON products.ProductID = pe.ProductID
LEFT JOIN orderdetails INNER JOIN orders ON orderdetails.OrderID = orders.OrderID ON orderdetails.productcode = products.productcode
and orders.OrderStatus <>'Cancelled' and orders.OrderStatus <>'Shipped' and orders.OrderStatus <>'Returned'