简体   繁体   English

获取一个表中计数大于另一表总和的所有记录

[英]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.

相关问题 在SQL Server中从子表中获取计数大于0的记录 - get records from child table having count greater than 0 in sql server 显示所有计数大于1的表记录 - Display all the table records whose count is greater than 1 MySQL:如何从一个表中查找日期晚于其他表的日期的记录计数 - MySQL: How to find a count of records from one table where date is later than date from other table 从不止一个表中具有相同列的所有记录 - Get all records from more than one table with the same columns SQL:从一个表获取所有记录和从第二个表中获取记录数? - SQL: Get all records from one table AND a count of records from a second table? 从一个表中选择项,且记录的总和与子查询中的HAVING相匹配 - select items from one table with sum of records matching HAVING in subquery MySQL:使用带有 WHERE 子句的 JOINS 从一个表中获取所有记录并从另一个表中获取可用记录 - MySQL: Using JOINS with WHERE clause to get all records from one table and available records from the other MySQL-对一个表中的记录进行计数,并对另一个表中的记录求和 - MySQL - Count records in one table and sum records in another table MySql获取id = xxx且id相同的行中其他表中的最新列大于一个的所有行 - MySql get all rows where id = xxx and where the newest column in other table in row with same id is greater than one 从记录的字段之一之和大于 - Get record ids from groups where the sum of one of the field of their records is greater than
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM