执行查询时按错误分组

时间:2016-09-16 10:41:37

标签: oracle

Select To_char(x.Fld004, 'dd/mm/yyyy hh24'),
       max(decode(fld008,1,count(1),null)) as aa,
       max(decode(fld008,2,count(1),null)) as bb
  From Pti020 x, Pti042 y
 Where x.Fld008 = y.Fld001
   And x.fld004 < trunc(sysdate-1)
   and x.fld004 > trunc(sysdate-2)
 Group By  To_char(x.Fld004, 'dd/mm/yyyy hh24')
 order by To_char(x.Fld004, 'dd/mm/yyyy hh24'), aa, bb;

为什么我在执行时遇到00937错误?怎么解决?

1 个答案:

答案 0 :(得分:0)

您通常不会嵌套聚合函数。也许您打算更像这样的查询:

select To_char(x.Fld004, 'dd/mm/yyyy hh24'),
       sum(case when fld008 = 1 then 1 else 0 end) as aa,
       sum(case when fld008 = 2 then 1 else 0 end) as bb
from Pti020 x join
     Pti042 y
     on x.Fld008 = y.Fld001
where x.fld004 < trunc(sysdate-1) and
      x.fld004 > trunc(sysdate-2)
group By to_char(x.Fld004, 'dd/mm/yyyy hh24')
order by To_char(x.Fld004, 'dd/mm/yyyy hh24');

以下是更改:

  • ,被显式JOIN取代。您应该使用正确的显式JOIN语法。
  • 聚合函数计算两列的值“1”和“2”。如果您只想要一个标记,请使用max()代替sum()
  • 我从aa删除了bborder by,因为group by保证第一列是唯一的。