Oracle Group通过查询

时间:2013-01-21 14:45:48

标签: oracle oracle10g oracle11g

我正在尝试运行以下查询:

SELECT parent_id, id, due_date, object_subtype
  FROM amatia_logtask
 WHERE parent_id IN (SELECT id_actividad
                       FROM amatia_actividades
                      WHERE id_actividad = '12624'
                         OR id_padre = '12624')
 GROUP BY parent_id id

但是,我无法理解错误消息:

ORA-00933: SQL command not properly ended
00933. 00000 -  "SQL command not properly ended"
*Cause:    
*Action:
Error at Line: 9 Column: 41

请您提供解释吗?

4 个答案:

答案 0 :(得分:3)

GROUP BY中有两列,它们之间没有逗号:

group by parent_id, id
                  ^ --- missing this

但是如果您尝试对列进行分组,那么您可以GROUP BY所有字段:

 group by parent_id, id, due_date, object_subtype

答案 1 :(得分:2)

目前尚不清楚您的目标是什么 - 以下内容将为您提供的明确列表:

SELECT DISTINCT y.parent_id, y.id, y.due_date, y.object_subtype
  FROM amatia_logtask y
 WHERE y.parent_id IN (SELECT x.id_actividad
                         FROM amatia_actividades x
                        WHERE '12624' IN (x.id_actividad, x.id_padre))

Oracle不允许您省略GROUP BY中的列,因此任何需要包含在聚合函数中的列都是如此。 IE:

SELECT y.parent_id, y.id, 
       MAX(y.due_date) AS due_date, MIN(y.object_subtype) AS object_subtype
  FROM amatia_logtask y
 WHERE y.parent_id IN (SELECT x.id_actividad
                         FROM amatia_actividades x
                        WHERE '12624' IN (x.id_actividad, x.id_padre))
GROUP BY y.parent_id, y.id

答案 2 :(得分:2)

GROUP BY子句是以逗号分隔的列或表达式列表,因此您缺少逗号。

您仍然会收到错误,因为SELECT子句中的所有表达式都需要按分组或聚合(使用aggregate functions)。

所以你可能想写:

SELECT parent_id, id, due_date, object_subtype
  FROM amatia_logtask
 WHERE parent_id IN (SELECT id_actividad
                       FROM amatia_actividades
                      WHERE id_actividad = '12624'
                         OR id_padre = '12624')
 GROUP BY parent_id, id, due_date, object_subtype

没有聚合的GROUP BY查询本质上是DISTINCT查询,因此您也可以在此使用distinct来暗示您的意图:

SELECT DISTINCT parent_id, id, due_date, object_subtype
  FROM amatia_logtask
 WHERE parent_id IN (SELECT id_actividad
                       FROM amatia_actividades
                      WHERE id_actividad = '12624'
                         OR id_padre = '12624')

答案 3 :(得分:0)

试试这个Plz

SELECT parent_id, id, due_date, object_subtype
  FROM amatia_logtask
 WHERE parent_id IN (SELECT id_actividad
                       FROM amatia_actividades
                      WHERE id_actividad = '12624'
                         OR id_padre = '12624')
 GROUP BY parent_id,id

你在GROUP BY Clause.Good Luck中错过了逗号