ORA-00979:此查询中不是GROUP BY表达式

时间:2014-02-12 15:05:20

标签: sql oracle

我有以下代码[感谢Gordon Linoff]:

   select (case when n.n = 1 then column1 else 'END' end) as column1,
           (case when n.n = 1 then firsttime else lasttime end) as "time"
    from (select column1, min(time) as firsttime, max(time) as lasttime
          from t
          group by column1
         ) t cross join
         (select 1 as n from dual union all select 2 from dual) n
    order by column1, n.n;

表“t”中有更多列,因此我尝试使用以下查询:

   select (case when n.n = 1 then column1 else 'END' end) as column1,
           (case when n.n = 1 then firsttime else lasttime end) as "time",
           column2, column3
    from (select column1,column2,column3, min(time) as firsttime, max(time) as lasttime
          from t
          group by column1
         ) t cross join
         (select 1 as n from dual union all select 2 from dual) n
    order by column1, n.n;

但“ORA-00979: not a GROUP BY expression”是输出。

1 个答案:

答案 0 :(得分:2)

您还需要将其他列添加到group by

select (case when n.n = 1 then column1 else 'END' end) as column1,
       (case when n.n = 1 then firsttime else lasttime end) as "time",
       column2, column3
from (select column1,column2,column3, min(time) as firsttime, max(time) as lasttime
      from t
      group by column1, column2, column3
     ) t cross join
     (select 1 as n from dual union all select 2 from dual) n
order by column1, n.n;

或者,您可以在select

中使用聚合函数
from (select column1, min(column2) as column2, min(column3) as column3, . . .