简体   繁体   English

如何在此查询中运行GROUP BY子句?

[英]How do I make the GROUP BY clause in this query run?

Write a SELECT statement that returns three columns: 编写一个SELECT语句返回三列:

EmailAddress , OrderID , and the order total for each customer . 每个customer EmailAddressOrderIDorder total

To do this, you can group the result set by the EmailAddress and OrderID columns. 为此,您可以按EmailAddressOrderID列对结果集进行分组。

In addition, you must calculate the order total from the columns in the OrderItems table. 另外,您必须从OrderItems表中的列中计算订单总数。

SELECT c.EmailAddress, oi.OrderID, (oi.ItemPrice * oi.Quantity) - 
oi.DiscountAmount
FROM Customers c JOIN Orders o ON c.CustomerID = o.CustomerID
                 JOIN OrderItems oi ON o.OrderID = oi.OrderID
GROUP BY c.EmailAddress, oi.OrderID

You are looking for GROUP BY and SUM() : 您正在寻找GROUP BYSUM()

SELECT c.EmailAddress, oi.OrderID,
       SUM(oi.ItemPrice * oi.Quantity - oi.DiscountAmount)
FROM Customers c JOIN
     Orders o
     ON c.CustomerID = o.CustomerID JOIN
     OrderItems oi
     ON o.OrderID = oi.OrderID
GROUP BY c.EmailAddress, oi.OrderID;

Column must appear in the GROUP BY clause or be used in an aggregate function. 列必须出现在GROUP BY子句中或在聚合函数中使用。 Your column 您的专栏

(oi.ItemPrice * oi.Quantity) - oi.DiscountAmount

does not satisfy this requirement. 不满足此要求。 I assume what you want to do is 我想你想做的是

sum((oi.ItemPrice * oi.Quantity) - oi.DiscountAmount) 

?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在以下查询中,如何在where子句中使用“ group”进行比较? - How do I use the `group` in where clause for the comparison in following query? 如何使用GROUP BY子句将查询移植到PostgreSQL? - How do I port query with GROUP BY clause to PostgreSQL? 如何限制ActiveRecord查询,然后运行“ where”子句? - How do I limit an ActiveRecord query, then run a “where” clause? Django:如何使用HAVING子句明确进行查询? - Django: How do I explicitly make a query with a HAVING clause? 如何使此查询运行得更快? - how do I make this query run faster? 如何使用FILTER子句进行查询运行得更快? - How to make a query with FILTER clause run faster? 如何使WHERE子句中的IN查询更快地运行 - How to make Query with IN in WHERE clause run faster 使用group by子句时,如何在Linq查询中进行嵌套投影? - How can I make a nested projection in a Linq query when using the group by clause? 如何运行此 SQL 服务器查询以按部门分组 - How do I run this SQL Server query to group by department 如何在具有Group By子句的SQL查询的where子句中使用算术表达式,而不会出现算术溢出? - How do I use an arithmetic expression in the where clause of a SQL query that has a Group By clause without getting arithmetic overflow?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM