Oracle组只有一列

时间:2017-05-11 15:37:54

标签: sql oracle group-by

我在Oracle数据库中有一个表,它有40列。 我知道如果我想通过查询执行分组,则select中的所有列都必须在分组中。

我只想做:

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中,不可能只按一列分组吗?

更新

道歉,因为不够清楚。

我的表:

+--------+----------+-------------+-------+
| 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   |
+--------+----------+-------------+-------+

3 个答案:

答案 0 :(得分:5)

这里有长篇评论;

是的,你不能这样做。想一想......如果你有这样的表:

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 = 2MIN()
  • co1,您只想查看不同的值,因此GROUP BY
  • col2,您只想查看不同的值,因此GROUP BY
  • col3,您只想查看不同的值,因此GROUP BY

答案 1 :(得分:1)

为什么你想要GROUP BY,不想要ORDER BY呢?

如果您声明要解决的问题的英语版本(即要求),则更容易更具体。

答案 2 :(得分:0)

也许分析功能就是你需要的

像这样试试smth:

select col1, col2, col3, col4, col5 
, sum(*) over (partition by col1) as col1_summary
, count(*) over () as total_count
from t1 

如果你谷歌文章 - 你会发现成千上万的例子 例如这个 Introduction to Analytic Functions (Part 1)