[英]Get all records having count from one table is greater than sum of other table
I have three tables 1) CustomerOrders , 2) StockItems and 3) OrderContentsLine .我有三个表 1) CustomerOrders ,2) StockItems和 3) OrderContentsLine 。 StockItems
have customerorderid
(one to many relationship with CustomerOrders
) and OrderContentsLine
contains order items with item quantity (obviously one to many relationship with CustomerOrders
). StockItems
具有customerorderid
(与CustomerOrders
一对多关系)并且OrderContentsLine
包含具有项目数量的订单项目(显然与CustomerOrders
一对多关系)。
Now I want to get All orders which have sum of quantity from OrderContentsLine
table greater than count of StockItems
现在我想从OrderContentsLine
表中获取数量总和大于StockItems
数量的所有订单
myquery looks like this我的查询看起来像这样
select co.OrderNumber,si.SalesOrderID, sum(ocl.Quantity) Ordered, count(si.SalesOrderID) Allocated from CustomerOrders co
inner join StockItems si on co.OrderID = si.SalesOrderID
inner join OrderContentsLine ocl on ocl.OrderID=co.OrderID
where co.CompanyId=531
group by si.SalesOrderID,co.OrderNumber
having count(si.SalesOrderID)>sum(ocl.Quantity)
but this query shows no results, and I am damn sure that many orders have greater order conterntline items than sum of quantity from StockItems table.但是这个查询没有显示任何结果,而且我很确定许多订单的订单上下文项大于 StockItems 表中的数量总和。
Can you please review my query and suggest the better way to get these orders!您能否查看我的查询并建议获得这些订单的更好方法!
My required output is我需要的输出是
NOTE: this output is not generated by query !注意:此输出不是由查询生成的!
I have just created a query that gives me the required output我刚刚创建了一个查询,它为我提供了所需的输出
select * from(
select co.OrderNumber, co.OrderID, co.OrderStatus,
(select sum(tbl.Quantity) from OrderContentsLine tbl where tbl.OrderID=co.OrderID) Ordered,
(select count(*) from StockItems tbl2 where tbl2.SalesOrderID=co.OrderID ) Allocated
from CustomerOrders co
)temp where temp.Allocated> temp.Ordered
Your problem is the multiple one-to-many joins.您的问题是多个一对多连接。 You are counting and summing duplicates.您正在对重复项进行计数和求和。 For example, if you have 1 order with 2 stock items, and 3 order lines, your join of the three tables will have 6 rows.例如,如果您有 1 个包含 2 个库存项目的订单和 3 个订单行,则三个表的连接将有 6 行。 You have no relationship between StockItem and OrderContentsLine, so you get a cartesian product.您在 StockItem 和 OrderContentsLine 之间没有关系,因此您得到一个笛卡尔积。
You probably want something like你可能想要类似的东西
WITH ord AS
(
SELECT co.CompanyId, co.OrderID, co.OrderNumber, SUM(ocl.Quantity) AS Ordered
FROM CustomerOrders co
INNER JOIN OrderContentsLine ocl ON ocl.OrderID = co.OrderID
GROUP BY co.CompanyId, co.OrderNumber
), al AS
(
SELECT co.CompanyId, co.OrderID, co.OrderNumber, COUNT(si.SalesOrderID) AS Allocated
FROM CustomerOrders co
INNER JOIN StockItems si ON co.OrderID = si.SalesOrderID
GROUP BY co.CompanyId, co.OrderNumber
)
SELECT ord.CompanyId, ord.OrderNumber, ord.Ordered, al.Allocated
FROM ord
INNER JOIN al ON ord.OrderID = al.OrderID
WHERE companyId = 531
AND al.Allocated > ord.Ordered
Obviously hard to test with no data显然很难在没有数据的情况下进行测试
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.