假设我有一个包含以下各列的订单表:Model
Quantity
Price
ScheduleB
OrderID
可以有多个Model
具有相同的ScheduleB
分类。我需要一条SQL语句,该语句仅返回一条记录,其中对Quantity
进行相似分组的所有Price
分类的所有Model
总价格(ScheduleB
* OrderID
)大于$ 2500。有数百种不同的ScheduleB
分类可能。因此,您可能在表中看到的OrderID
= 10054的示例为:+-------------------------------------------+
| Model | Qty | Price | ScheduleB | OrderID |
+-------------------------------------------+
Dr1625, 2, $1298.87, 1029202938, 10054
Dr1624, 1, $123.87, 1029202930, 10054
Dr1623, 5, $2499.87, 1029202931, 10054
Dr1622, 3, $600.87, 1029202938, 10054
Dr1621, 1, $3298.87, 1029202938, 10054
ScheduleB
等于1029202938的记录的总和大于$ 2500,我希望返回以下内容:+-------------------------------------------+
| Model | Qty | Price | ScheduleB | OrderID |
+-------------------------------------------+
Dr1625, 2, $1298.87, 1029202938, 10054
Dr1622, 3, $600.87, 1029202938, 10054
Dr1621, 1, $3298.87, 1029202938, 10054
基本上,我只想显示来自同一ScheduleB
分类的总价格大于$ 2500的特定OrderID
的表中的记录。可以使用MYSQL中的SQL语句完成此操作吗?
编辑:
这是我用来获取上述列(以及其他一些列)的SQL语句:select brands.Brand, products.Model_PartNumber, categories.CategoryDescription, (Select Sum(Quantity) From orderitems where OrderID = 10054 AND ProductID = products.ProductID Group BY ProductID) as Quantity, orderitems.ItemPrice, brands.CountryOrigin, categories.ScheduleB, products.Weight, products.WeightIn from orderitems INNER JOIN products ON products.ProductID = orderitems.ProductID INNER JOIN brands ON products.BrandID = brands.BrandID LEFT JOIN productcategories ON productcategories.ProductID = products.ProductID INNER Join categories ON productcategories.CategoryID = categories.CategoryID WHERE orderitems.OrderID = 10054 Group By products.Model_partNumber
最佳答案
是的,可以这样做:
Select
Model
,Quantity
,Price
,ScheduleB
,OrderID
From Table1
WHERE ScheduleB IN
(SELECT ScheduleB FROM Table1 GROUP BY ScheduleB HAVING SUM(Quantity * Price) > 2500)
关于mysql - SQL语句-基于列类型的总和,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20964039/