sql - Oracle 仅按一列分组

标签 sql oracle group-by

我在 Oracle 数据库中有一个表,它有 40 列。 我知道如果我想进行 group by 查询,select 中的所有列都必须在 group by 中。

我只是想做:

select col1, col2, col3, col4, col5 from table group by col3

如果我尝试:

select col1, col2, col3, col4, col5 from table group by col1, col2, col3, col4, col5

它没有给出所需的输出。

我已经搜索过这个,但没有找到任何解决方案。我使用某种 Add() 或 count(*) 函数找到的所有查询。

在 Oracle 中不能简单地按一列分组吗?

更新:

抱歉,不够清楚。

我的 table :

+--------+----------+-------------+-------+
| id     | col1     | col2        | col3  |
+--------+----------+-------------+-------+
| 1      | 1        | some text 1 | 100   |
| 2      | 1        | some text 1 | 200   |
| 3      | 2        | some text 1 | 200   |
| 4      | 3        | some text 1 | 78    |
| 5      | 4        | some text 1 | 65    |
| 6      | 5        | some text 1 | 101   |
| 7      | 5        | some text 1 | 200   |
| 8      | 1        | some text 1 | 200   |
| 9      | 6        | some text 1 | 202   |
+--------+----------+-------------+-------+

并通过运行以下查询:

select col1, col2, col3 from table where col3='200' group by col1;

我将得到以下所需的输出:

+--------+----------+-------------+-------+
| id     | col1     | col2        | col3  |
+--------+----------+-------------+-------+
| 2      | 1        | some text 1 | 200   |
| 3      | 2        | some text 1 | 200   |
| 7      | 5        | some text 1 | 200   |
+--------+----------+-------------+-------+

最佳答案

这里长评;

是的,你不能那样做。想想看……如果你有这样的 table :

Col1 Col2 Col3
A    A    1
B    A    2
C    A    3

而您仅按 Col2 进行分组,这将分组为一行... Col1Col3 会发生什么?两者都有 3 个不同的行值。 您的 DBMS 应该如何显示这些?

Col1 Col2 Col3
A?   A    1?
B?        2?
C?        3?

这就是为什么您必须按所有列分组,或者以其他方式聚合或连接它们。 (SUM()MAX()MIN() 等。)

向我们展示您希望结果的外观,我相信我们可以为您提供帮助。

编辑 - 答案:

首先,感谢您更新您的问题。您的查询没有 id 但您的预期结果有,所以我将分别回答每个问题。

没有id

您仍然需要按所有列分组以实现您的目标。让我们来看看吧。

如果您在没有任何分组的情况下运行查询:

select col1, col2, col3 from table where col3='200'

你会得到这个:

+----------+-------------+-------+
| col1     | col2        | col3  |
+----------+-------------+-------+
| 1        | some text 1 | 200   |
| 2        | some text 1 | 200   |
| 5        | some text 1 | 200   |
| 1        | some text 1 | 200   |
+----------+-------------+-------+

所以现在您只想看到 col1 = 1 行一次。但要这样做,您需要将所有列向上滚动,以便您的 DBMS 知道如何处理每个列。如果您尝试仅按 col1 进行分组,您的 DBMS 将出现错误,因为您没有告诉它如何处理 col2col3 中的额外数据:

select col1, col2, col3 from table where col3='200' group by col1 --Errors

+----------+-------------+-------+
| col1     | col2        | col3  |
+----------+-------------+-------+
| 1        | some text 1 | 200   |
| 2        | some text 1 | 200   |
| 5        | some text 1 | 200   |
| ?        | some text 1?| 200?  |
+----------+-------------+-------+

如果您按所有 3 个分组,您的 DBMS 知道将整行分组(这是您想要的),并且只会显示一次重复的行:

select col1, col2, col3 from table where col3='200' group by col1, col2, col3

+----------+-------------+-------+
| col1     | col2        | col3  |
+----------+-------------+-------+
| 1        | some text 1 | 200   |
| 2        | some text 1 | 200   | --Desired results
| 5        | some text 1 | 200   |
+----------+-------------+-------+

带有id

如果您想查看 id,您必须告诉您的 DBMS 要显示哪个 id。即使我们按所有列分组,您也不会得到您想要的结果,因为 id 列将使每一行都不同(它们将不再组合在一起):

select id, col1, col2, col3 from table where col3='200' group by id, col1, col2, col3

+--------+----------+-------------+-------+
| id     | col1     | col2        | col3  |
+--------+----------+-------------+-------+
| 2      | 1        | some text 1 | 200   | --id = 2
| 3      | 2        | some text 1 | 200   |
| 7      | 5        | some text 1 | 200   |
| 8      | 1        | some text 1 | 200   | --id = 8
+--------+----------+-------------+-------+

因此,为了对这些行进行分组,我们需要明确说明如何处理 id。根据你想要的结果,你想选择id = 2,也就是最小值 id,所以让我们使用MIN() :

select MIN(id), col1, col2, col3 from table where col3='200' group by col1, col2, col3
--Note, MIN() is an aggregate function, so id need not be in the group by

返回你想要的结果(带有id):

+--------+----------+-------------+-------+
| id     | col1     | col2        | col3  |
+--------+----------+-------------+-------+
| 2      | 1        | some text 1 | 200   |
| 3      | 2        | some text 1 | 200   |
| 7      | 5        | some text 1 | 200   |
+--------+----------+-------------+-------+

最后的想法

这是你的两个麻烦行:

+--------+----------+-------------+-------+
| id     | col1     | col2        | col3  |
+--------+----------+-------------+-------+
| 2      | 1        | some text 1 | 200   |
| 8      | 1        | some text 1 | 200   |
+--------+----------+-------------+-------+

任何时候你点击这些,只要想想你希望每一列做什么,一次一个。每次进行分组或聚合时,您都需要处理所有列。

  • id,你只想看到id = 2,也就是MIN()
  • co1,你只想看到不同的值,所以 GROUP BY
  • col2,你只想看到不同的值,所以 GROUP BY
  • col3,你只想看到不同的值,所以 GROUP BY

关于sql - Oracle 仅按一列分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43919957/

相关文章:

sql - 错误 : invalid input syntax for type numeric: "" (postgresql)

sql - 在 Oracle 数据库中搜索带有转义符的字符串

java - 确保事务的 ACID 属性的责任在哪里?

oracle - PL/SQL XML 解析为关系表

mysql - MySQL 中的这个 group by 查询有什么问题?

php - sql查询具有特定标签的帖子

c# - 如何在脚本组件中访问 ssis 包变量

oracle - 如何在11g中的select语句中为新创建的表的列设置默认值

mysql - sql在按其他列分组后选择并显示一列的所有值

MySQL 对分组 (GROUP BY) 结果使用 ORDER BY