gpt4 book ai didi

sql - 在完全理解 GROUP BY 时遇到问题

转载 作者:行者123 更新时间:2023-12-04 11:27:32 25 4
gpt4 key购买 nike

我正在复习我即将举行的考试的一些练习题,但我在完全理解 group by 时遇到了问题。我看到 GROUP BY 如下所示:按一列或多列对结果集进行分组。

我有以下数据库架构

enter image description here

enter image description here

我的查询

SELECT orders.customer_numb, sum(order_lines.cost_line), customers.customer_first_name, customers.customer_last_name
FROM orders
INNER JOIN customers ON customers.customer_numb = orders.customer_numb
INNER JOIN order_lines ON order_lines.order_numb = orders.order_numb
GROUP BY orders.customer_numb, order_lines.cost_line, customers.customer_first_name, customers.customer_last_name
ORDER BY order_lines.cost_line DESC

我难以理解的内容
为什么我不能简单地使用 GROUP BY orders.cost_line并按 cost_line 对数据进行分组?

我正在努力实现的目标
我想实现花费最多的客户的名称。我只是不完全理解如何实现这一目标。我了解连接是如何工作的,我似乎无法理解为什么我不能简单地按 customer_numb 和 cost_line 进行分组(使用 sum() 来计算花费的金额)。我似乎总是得到“不是 GROUP BY 表达式”,如果有人能解释我做错了什么(不仅仅是给我答案),那就太好了 - 我真的很感激,当然还有任何资源您必须正确使用 GROUP。

对不起,这篇长文,如果我错过了什么,我道歉。任何帮助将不胜感激。

最佳答案

I just can't seem to get my head around why I can't simply GROUP BY customer_numb and cost_line (with sum() used to calculate the amount spent).



当你说 group by customer_numb您知道 customer_numb 唯一标识了客户表中的一行(假设 customer_numb 是主键或备用键),因此任何给定的 customers.customer_numb customers.customer_first_name 将只有一个值和 customers.customer_last_name .但是在解析时 Oracle 不知道,或者至少表现得好像它不知道。它有点慌张地说:“如果一个 customer_numb 有多个 customer_first_name 的值,我该怎么办?”

大致规则是, select 中的表达式子句可以使用 group by 中的表达式子句和/或使用聚合函数。 (以及不依赖于基表的常量和系统变量等。)“使用”是指表达式或表达式的一部分。因此,一旦您按名字和姓氏分组, customer_first_name || customer_last_name也将是一个有效的表达。

当你有一张 table 时,比如 customers并且按主键或具有唯一键且非空约束的列分组,您可以安全地将它们包含在 group by 中条款。在此特定实例中, group by customer.customer_numb, customer.customer_first_name, customer.customer_last_name.
另请注意, order by在第一个查询中会失败,因为 order_lines.cost_line该组没有单一值。您可以在 sum(order_lines.cost_line) 订购或在 select 中使用列别名条款和命令 alias
SELECT orders.customer_numb, 
sum(order_lines.cost_line),
customers.customer_first_name,
customers.customer_last_name
FROM orders
INNER JOIN customers ON customers.customer_numb = orders.customer_numb
INNER JOIN order_lines ON order_lines.order_numb = orders.order_numb
GROUP BY orders.customer_numb,
customers.customer_first_name,
customers.customer_last_name
ORDER BY sum(order_lines.cost_line)

或者
SELECT orders.customer_numb, 
sum(order_lines.cost_line) as sum_cost_line,
. . .
ORDER BY sum_cost_line

注意:我听说有些 RDBMS 会在没有明确说明的情况下暗示分组的其他表达式。 Oracle 不是那些 RDBMS 之一。

至于按两者分组 customer_numbcost_line考虑一个有两个客户的数据库,1 和 2 有两个订单,每个订单一行:
Customer Number | Cost Line
1 | 20.00
1 | 20.00
2 | 35.00
2 | 30.00

select customer_number, cost_line, sum(cost_line)
FROM ...
group by customer_number, cost_line
order by sum(cost_line) desc

Customer Number | Cost Line | sum(cost_line)
1 | 20.00 | 40.00
2 | 35.00 | 35.00
2 | 30.00 | 30.00

第一行最高 sum(cost_line)不是花费最多的客户。

关于sql - 在完全理解 GROUP BY 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13022001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com