ORA-0097按错误分组

时间:2014-03-07 19:34:55

标签: sql oracle

我在程序中有这个select语句:

SELECT activity_note, maint,hist_maint, position 
FROM   (SELECT activity_note, activity_count, maint, hist_maint, position
        FROM   (SELECT (CASE WHEN rfi IN (SELECT column_value 
                                          FROM   table(v_sum_pms_rfi_list))
                             THEN set_gid || '-PM-' || activity_note else set_gid || '-' || activity_note end) AS activity_note,
                       SUM(activity_count) AS ctivity_count
                FROM   dpt_production_activities
                WHERE  set_gid IN (1428, 1666)
                  AND  rfi IN (SELECT column_value FROM table(v_sum_pms_rfi_list))
                   OR  rfi in (SELECT column_value FROM table(v_sum_rfi_list))
                  AND  activity_code = 'COUNT'
                GROUP  BY activity_note, activity_count)

我错误地收到了无效组,ORA-0097。我的理解是,我需要在组中选择所有选定的列,并选择聚合列 - 所以我用select语句提供两个变量(activity_note和activity_count) - 前者带有条件,后者带有条件汇总列

如何将这些放入我的小组中?我偶然发现了一些不可行的东西,或者是一种我看不到的优雅解决方案

我应该提一下,之前的版本工作正常,内部select语句不包含case语句而GROUP BYset_gid || '-' || activity_note

1 个答案:

答案 0 :(得分:1)

试试这个

   SELECT activity_note, maint,hist_maint, position 
      FROM 
          (
           SELECT activity_note, SUM(activity_count) AS ctivity_count
                 FROM (
                       SELECT (CASE WHEN rfi IN 
                                   (
                                    SELECT column_value 
                                           FROM table(v_sum_pms_rfi_list)
                                    ) THEN    
                                     set_gid || '-PM-' || activity_note else set_gid || '-' || activity_note end) AS activity_note, activity_count
                       FROM dpt_production_activities)
                 WHERE set_gid in(1428, 1666)
                      AND rfi IN 
                              (SELECT column_value 
                                    FROM table(v_sum_pms_rfi_list)) 
                      OR rfi IN (SELECT column_value 
                                    FROM table(v_sum_rfi_list))
                      AND activity_code = 'COUNT'
                 GROUP BY activity_note))