SQL组问题

时间:2010-10-14 14:05:09

标签: sql oracle ora-00979

我的查询显示ORA-00979:not a GROUP BY expression错误。

我不是一个sql pro所以请你能给我一个提示有什么不对吗?

SELECT *
  FROM (SELECT a.*, ROWNUM r___
          FROM (SELECT DISTINCT "SMTAPP"."XF_MASTER"."ID" AS "XfMasterEntityId",
                                "SMTAPP"."XP_MASTER_DETAIL"."WIFENAME" AS "XpMasterDetailEntityWifename",
                                "SMTAPP"."XP_MASTER_DETAIL"."COUNT" AS "XpMasterDetailEntityCount",
                                MAX ("SMTAPP"."XP_MASTER_DETAIL"."COUNT") AS "XpMasterDetailEntityCountMax"
                           FROM ("SMTAPP"."XF_MASTER" LEFT JOIN "SMTAPP"."XP_MASTER_DETAIL"
                                ON "SMTAPP"."XF_MASTER"."ID" =  "SMTAPP"."XP_MASTER_DETAIL"."XF_MASTER_ID")
                       GROUP BY "SMTAPP"."XP_MASTER_DETAIL"."COUNT"
                       ORDER BY "SMTAPP"."XF_MASTER"."ID" ASC) a
         WHERE ROWNUM < '11')
 WHERE r___ >= '1'

4 个答案:

答案 0 :(得分:3)

使用GROUP BY时,您选择的所有列必须使用聚合函数进行聚合,或者位于GROUP BY子句中。否则,您将拥有该列的项目列表,这不是单个值。

因此请尝试此操作(用现有的GROUP BY替换):

GROUP BY "SMTAPP"."XF_MASTER"."ID", 
  "SMTAPP"."XP_MASTER_DETAIL"."WIFENAME", 
  "SMTAPP"."XP_MASTER_DETAIL"."COUNT"

答案 1 :(得分:2)

尝试将SELECT中的其他列添加到GROUP BYIDWIFENAME

答案 2 :(得分:1)

在最内层的查询

SELECT DISTINCT 
"SMTAPP"."XF_MASTER"."ID" AS "XfMasterEntityId",
"SMTAPP"."XP_MASTER_DETAIL"."WIFENAME" AS "XpMasterDetailEntityWifename",
"SMTAPP"."XP_MASTER_DETAIL"."COUNT" AS "XpMasterDetailEntityCount",
MAX ("SMTAPP"."XP_MASTER_DETAIL"."COUNT") AS "XpMasterDetailEntityCountMax"

FROM ("SMTAPP"."XF_MASTER" LEFT JOIN "SMTAPP"."XP_MASTER_DETAIL"
ON "SMTAPP"."XF_MASTER"."ID" =  "SMTAPP"."XP_MASTER_DETAIL"."XF_MASTER_ID")
GROUP BY "SMTAPP"."XP_MASTER_DETAIL"."COUNT"
ORDER BY "SMTAPP"."XF_MASTER"."ID" ASC

您在SELECT中有列,即

"SMTAPP"."XF_MASTER"."ID" AS "XfMasterEntityId",

AND

"SMTAPP"."XP_MASTER_DETAIL"."WIFENAME" AS "XpMasterDetailEntityWifename"

不属于GROUP BY "SMTAPP"."XP_MASTER_DETAIL"."COUNT"且不允许使用。

您只能在SELECT中拥有属于Group By或使用MAX / AVG等聚合函数聚合的列

答案 3 :(得分:1)

@eumiro的意思是

   SELECT *
      FROM (SELECT a.*, ROWNUM r___
              FROM (SELECT DISTINCT "SMTAPP"."XF_MASTER"."ID" AS "XfMasterEntityId",
                                    "SMTAPP"."XP_MASTER_DETAIL"."WIFENAME" AS "XpMasterDetailEntityWifename",
                                    "SMTAPP"."XP_MASTER_DETAIL"."COUNT" AS "XpMasterDetailEntityCount",
                                    MAX ("SMTAPP"."XP_MASTER_DETAIL"."COUNT") AS "XpMasterDetailEntityCountMax"
                               FROM ("SMTAPP"."XF_MASTER" LEFT JOIN "SMTAPP"."XP_MASTER_DETAIL"
                                    ON "SMTAPP"."XF_MASTER"."ID" =  "SMTAPP"."XP_MASTER_DETAIL"."XF_MASTER_ID")
                           GROUP BY "SMTAPP"."XF_MASTER"."ID", "SMTAPP"."XP_MASTER_DETAIL"."WIFENAME", "SMTAPP"."XP_MASTER_DETAIL"."COUNT"
                           ORDER BY "SMTAPP"."XF_MASTER"."ID" ASC) a
             WHERE ROWNUM < '11')
     WHERE r___ >= '1'