Oracle GROUP BY 给出错误 ORA-00979:不是 GROUP BY 表达式
假设我们有两个表
table1
----------------------
|ID | Date |
----------------------
|1 |20110101|
|1 |20110102|
|1 |20110103|
|2 |20110102|
|2 |20110103|
|2 |20110104|
----------------------
table2
----------------------
|ID2 |val |
----------------------
|1 |152 |
|2 |155 |
----------------------
使用这个查询
SELECT * FROM table1, table2
WHERE table1.ID = table2.ID2
GROUP BY table1.ID
ORDER BY DATE DESC
Mysql应该返回这个
-------------------------------------------
|ID |date |ID2 |val |
-------------------------------------------
|1 |20110103|1 |152 |
|2 |20110104|2 |155 |
-------------------------------------------
在Oracle中我得到这个错误: ORA-00979:不是 GROUP BY 表达式
编辑: Date
列上的 MAX
函数不起作用,因为该列是 varchar(200)
数据库/表结构不是我的,我无法更改它。
let's assume we have two tables
table1
----------------------
|ID | Date |
----------------------
|1 |20110101|
|1 |20110102|
|1 |20110103|
|2 |20110102|
|2 |20110103|
|2 |20110104|
----------------------
table2
----------------------
|ID2 |val |
----------------------
|1 |152 |
|2 |155 |
----------------------
Using this query
SELECT * FROM table1, table2
WHERE table1.ID = table2.ID2
GROUP BY table1.ID
ORDER BY DATE DESC
Mysql should return this
-------------------------------------------
|ID |date |ID2 |val |
-------------------------------------------
|1 |20110103|1 |152 |
|2 |20110104|2 |155 |
-------------------------------------------
In Oracle I get this error:ORA-00979: not a GROUP BY expression
EDIT:
The MAX
function on the column Date
does not work because this column is varchar(200)
The database/tables structure is not mine and I cannot alter it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要执行两件事之一...
在您的情况下,您不只是想要来自 table1 的 MAX(),因为可能较高的
id
具有较低的日期
。在这种情况下,我倾向于使用查找系统...注意:这假设您的日期格式为字母数字排序将产生正确的日期顺序。如果情况并非如此(并且
dm-yyyy
将无法正确排序),您需要替换date_field
使用TO_DATE(date_field)
确保顺序正确。注意:使用
TO_DATE(date_field)
也可能会解决您的 MAX() 问题。注意:如果您想将日期存储为字符串,但为了顺序友好,请使用
yyyy-mm-dd
You need to do one of two things...
In your case, you don't just want a MAX() from table1, as it may be possible that a higher
id
has a lowerdate
. In that case, I'd be inclined to use a lookup system...NOTE: This assumes your date is formatted such that alphanumeric ordering WILL yield the correct date order. If that is NOT the case (and
d-m-yyyy
will not order correctly), you need to replacedate_field
withTO_DATE(date_field)
to ensure the correct order.NOTE: Use of
TO_DATE(date_field)
will also probably fix your MAX() problems.NOTE: If you want to store dates as strings, but them to be order friendly, use
yyyy-mm-dd
在带有 group by 的 SQL 语句的 select 和 order by 子句中,只能使用 group by 中使用的列/表达式或其他列的聚合函数(min、max、avg ..)。
我知道 Oracle 是这样,但令我感到有点惊讶的是 MySQL 中的情况有所不同
In the select and order by clause of a SQL Statement with group by you can only use the columns/expression used in the group by or aggregate functions (min, max, avg ..) of other columns.
I know this for oracle and I am a little surprised that this is different in MySQL
按以下方法尝试一下:
Try it in following way:
一般来说,将
select * from
与group by
结合使用是一个坏主意。 select 子句中的表达式需要是标量,即要么是组的聚合函数,要么与分组无关,要么是在 group by 子句本身中。Select *
几乎保证不会出现这种情况。你可能想要这样的东西:In general, saying
select * from
in combination withgroup by
is a bad idea. The expressions in the select clause need to be scalar, that is, either aggregate functions of the group, or unrelated to the grouping, or in the group by clause themselves.Select *
pretty much guarantees that that will not be the case. You probably want something like this instead:Oracle 中的 Group by 意味着您正在尝试对其中一列运行聚合函数(求和、计数等)。看来您只是想先按 ID 订购,然后再按日期订购。我会做这样的事情:
如果这不是您想要的,那么答案是您需要 select 子句中不属于聚合函数的每一列都位于 group 子句中。
编辑
响应评论:
最好首先将表1作为子查询进行过滤,然后连接到表2:
Group by in oracle implies that you are trying to run an aggregate function (sum, count, etc.) on one of the columns. It appears that you are just looking to order by ID first and then the date. I would do something like this:
If that is not what you are intending, then the answer is that you need each column in the select clause that is not part of an aggregate function to be in the group clause.
EDIT
In response to the comment:
It's probably best to filter table 1 down first as a subquery and then join to table2: