oracle 在oracle中选择*分组依据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25782524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 02:30:31  来源:igfitidea点击:

Select * group by in oracle

oracle

提问by gadelat

How would I write this simple mysql equivalent in oracle?

我将如何在 oracle 中编写这个简单的 mysql 等价物?

select * from table group by column

It seems it does not accept star char when groupping, but it's really not practical to select columns one by one when there are 30+ columns in table.

分组的时候好像不接受星号,但是当表中有30多列时,一一选择列确实不切实际。

采纳答案by gadelat

select * from table
WHERE rowid in
(SELECT MIN(rowid)
FROM table
GROUP BY column);

回答by Lalit Kumar B

You need to mention the column names explicitly for which you want to group. So, your SQL in Oracle would be :

您需要明确提及要为其分组的列名称。因此,您在 Oracle 中的 SQL 将是:

select column_list from table group by column_list

or,

或者,

select few/any columns from table group by same columns + more columns

Also, remember the column_list in select must be at least present in group by clause in the same order. You can select fewer columns and still group by those columns and additionally other columns too.

另外,请记住 select 中的 column_list 必须至少以相同的顺序出现在 group by 子句中。您可以选择更少的列,并且仍然按这些列和其他列进行分组。

For example,

例如,

select ename from emp group by ename,sal

回答by Alexio

If you want the same behaviour as in e.g. MySQL, you need to use an aggregate function, something like the MAX or MIN functions in oracle. Unfortunately, the only way I know to get the entire row back (rather than random field values from different rows) is to concatenate values together:

如果您想要与例如 MySQL 中的行为相同的行为,您需要使用聚合函数,例如 oracle 中的 MAX 或 MIN 函数。不幸的是,我知道获取整行(而不是来自不同行的随机字段值)的唯一方法是将值连接在一起:

SELECT
    col_to_group_by,
    MAX(col_1 || ', ' || col_2 || ', ' || col_3 || ', ' || col_4) row_values
FROM table_name
GROUP BY col_to_group_by