pro*_*bie 2 sql oracle group-by
我在Oracle数据库中有一个表,它有40列.我知道如果我想按查询进行分组,则select中的所有列必须在group by中.
我只是想做:
select col1, col2, col3, col4, col5 from table group by col3
Run Code Online (Sandbox Code Playgroud)
如果我尝试:
select col1, col2, col3, col4, col5 from table group by col1, col2, col3, col4, col5
Run Code Online (Sandbox Code Playgroud)
它没有提供所需的输出.
我搜索了这个,但没有找到任何解决方案.我使用某种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 |
+--------+----------+-------------+-------+
Run Code Online (Sandbox Code Playgroud)
并通过运行以下查询:
select col1, col2, col3 from table where col3='200' group by col1;
Run Code Online (Sandbox Code Playgroud)
我将得到以下所需的输出:
+--------+----------+-------------+-------+
| id | col1 | col2 | col3 |
+--------+----------+-------------+-------+
| 2 | 1 | some text 1 | 200 |
| 3 | 2 | some text 1 | 200 |
| 7 | 5 | some text 1 | 200 |
+--------+----------+-------------+-------+
Run Code Online (Sandbox Code Playgroud)
这里有长篇评论;
是的,你做不到.想一想......如果你有这样的桌子:
Col1 Col2 Col3
A A 1
B A 2
C A 3
Run Code Online (Sandbox Code Playgroud)
而你只分组Col2,这将组到一个单一的排...发生了什么Col1和Col3?这两个都有3个不同的行值.您的DBMS应该如何显示这些?
Col1 Col2 Col3
A? A 1?
B? 2?
C? 3?
Run Code Online (Sandbox Code Playgroud)
这就是您必须按所有列分组,或以其他方式聚合或连接它们的原因.(SUM(),MAX(),MIN()等.)
向我们展示您希望结果如何,我相信我们可以帮助您.
编辑 - 答案:
首先,感谢您更新您的问题.您的查询没有,id但您的预期结果没有,所以我将分别回答每个问题.
没有 id
您仍然需要按所有列进行分组才能实现您的目标.让我们来看看吧.
如果您在没有任何组的情况下运行查询:
select col1, col2, col3 from table where col3='200'
Run Code Online (Sandbox Code Playgroud)
你会得到这个:
+----------+-------------+-------+
| col1 | col2 | col3 |
+----------+-------------+-------+
| 1 | some text 1 | 200 |
| 2 | some text 1 | 200 |
| 5 | some text 1 | 200 |
| 1 | some text 1 | 200 |
+----------+-------------+-------+
Run Code Online (Sandbox Code Playgroud)
所以现在你只想看col1 = 1一次行.但要这样做,您需要向上滚动所有列,以便您的DBMS知道每个列的操作.如果试图通过组只col1,您DBMS将通过一个错误,因为你没有告诉它做什么用的额外的数据col2和col3:
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? |
+----------+-------------+-------+
Run Code Online (Sandbox Code Playgroud)
如果按所有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 |
+----------+-------------+-------+
Run Code Online (Sandbox Code Playgroud)
同 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
+--------+----------+-------------+-------+
Run Code Online (Sandbox Code Playgroud)
因此,为了对这些行进行分组,我们需要明确说明如何处理这些行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
Run Code Online (Sandbox Code Playgroud)
返回您想要的结果(带id):
+--------+----------+-------------+-------+
| id | col1 | col2 | col3 |
+--------+----------+-------------+-------+
| 2 | 1 | some text 1 | 200 |
| 3 | 2 | some text 1 | 200 |
| 7 | 5 | some text 1 | 200 |
+--------+----------+-------------+-------+
Run Code Online (Sandbox Code Playgroud)
最后的想法
这是你的两个麻烦行:
+--------+----------+-------------+-------+
| id | col1 | col2 | col3 |
+--------+----------+-------------+-------+
| 2 | 1 | some text 1 | 200 |
| 8 | 1 | some text 1 | 200 |
+--------+----------+-------------+-------+
Run Code Online (Sandbox Code Playgroud)
无论何时遇到这些问题,只需考虑每个列要做什么,一次一个.每次进行分组或聚合时,都需要处理所有列.
id,你只想看id = 2,哪个是MIN()co1,你只想看到不同的值,所以 GROUP BYcol2,你只想看到不同的值,所以 GROUP BYcol3,你只想看到不同的值,所以 GROUP BY