作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在复习我即将举行的考试的一些练习题,但我在完全理解 group by 时遇到了问题。我看到 GROUP BY 如下所示:按一列或多列对结果集进行分组。
我有以下数据库架构
我的查询
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 对数据进行分组?
最佳答案
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
也将是一个有效的表达。
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
customer_numb
和
cost_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/
我是一名优秀的程序员,十分优秀!