ORA-00937 使用 CASE 条件时不是单组函数

ORA-00937 not a single-group group function when using CASE Condition

我已经搜索了几个小时了,关于如何解决 ORA-00937: not a single-group group function and none of them似乎解释了为什么我收到错误。我的理解是,当在 select 语句中对一个或多个列应用聚合函数时,您应该包括未在 select by 语句中聚合的列。我已经这样做了,但是错误总是发生在第 2 行第 9 列,即 c.data_entry_date 位于下面第二行的位置。只有当我应用这部分查询的逻辑时才会发生此错误:

SUM(CASE 
    WHEN EXTRACT(YEAR FROM a.issue_date) < 1986 THEN
        CASE
          WHEN floor((c.data_entry_date - to_date(TO_CHAR(a.issue_date, 'DD-MON') || '-1986'))/365) between 0 and 5 then (7*(sum(a.area_in_hectares)))
          WHEN floor((c.data_entry_date - to_date(TO_CHAR(a.issue_date, 'DD-MON') || '-1986'))/365) between 6 and 10 then (10*(sum(a.area_in_hectares)))
          WHEN floor((c.data_entry_date - to_date(TO_CHAR(a.issue_date, 'DD-MON') || '-1986'))/365) between 11 and 15 then (15*(sum(a.area_in_hectares)))
          WHEN floor((c.data_entry_date - to_date(TO_CHAR(a.issue_date, 'DD-MON') || '-1986'))/365) between 16 and 20 then (20*(sum(a.area_in_hectares)))
          WHEN floor((c.data_entry_date - to_date(TO_CHAR(a.issue_date, 'DD-MON') || '-1986'))/365) between 21 and 25 then (25*(sum(a.area_in_hectares)))
          WHEN floor((c.data_entry_date - to_date(TO_CHAR(a.issue_date, 'DD-MON') || '-1986'))/365) between 26 and 30 then (30*(sum(a.area_in_hectares)))
          WHEN floor((c.data_entry_date - to_date(TO_CHAR(a.issue_date, 'DD-MON') || '-1986'))/365) between 31 and 35 then (35*(sum(a.area_in_hectares)))
          WHEN floor((c.data_entry_date - to_date(TO_CHAR(a.issue_date, 'DD-MON') || '-1986'))/365) between 36 and 40 then (40*(sum(a.area_in_hectares)))
          WHEN floor((c.data_entry_date - to_date(TO_CHAR(a.issue_date, 'DD-MON') || '-1986'))/365) between 41 and 45 then (45*(sum(a.area_in_hectares)))
          WHEN floor((c.data_entry_date - to_date(TO_CHAR(a.issue_date, 'DD-MON') || '-1986'))/365) between 46 and 50 then (50*(sum(a.area_in_hectares)))
        END
    END)

如果我要评论特定的 CASE 逻辑,那么查询会成功运行。也就是说,CASE 逻辑对我来说是相当新的。我尝试了一些创造性的谷歌搜索来尝试找到某种解决方案。我在 group by 中玩过各种专栏,但我看不到它能正常工作。我想知道 SUMCASE 逻辑中使用 c.data_entry_date 列是否与我在其中的使用方式冲突SELECT 语句中的第一列?如果有人想解决解决方案,这是我的更广泛的代码:

select 
TO_CHAR(c.data_entry_date,'yyyy-mm') as "Year-Month",
c.event_type_code "Event Code",
d.description "Event Description",
e.description "Title Type",
sum(a.area_in_hectares) "Sum hectares",
Count(unique(a.t_number_id)) "Count of T Number IDs",
CASE
  WHEN c.event_type_code = 'L_SPAY' and a.tenure_type_code = 'C' and a.tenure_sub_type_code = 'L' THEN (10*(sum(a.area_in_hectares)))

  WHEN c.event_type_code = 'L_SPAY' and a.tenure_type_code = 'C' and a.tenure_sub_type_code = 'S' THEN 
    SUM(CASE 
        WHEN EXTRACT(YEAR FROM a.issue_date) < 1986 THEN
            CASE
              WHEN floor((c.data_entry_date - to_date(TO_CHAR(a.issue_date, 'DD-MON') || '-1986'))/365) between 0 and 5 then (7*(sum(a.area_in_hectares)))
              WHEN floor((c.data_entry_date - to_date(TO_CHAR(a.issue_date, 'DD-MON') || '-1986'))/365) between 6 and 10 then (10*(sum(a.area_in_hectares)))
              WHEN floor((c.data_entry_date - to_date(TO_CHAR(a.issue_date, 'DD-MON') || '-1986'))/365) between 11 and 15 then (15*(sum(a.area_in_hectares)))
              WHEN floor((c.data_entry_date - to_date(TO_CHAR(a.issue_date, 'DD-MON') || '-1986'))/365) between 16 and 20 then (20*(sum(a.area_in_hectares)))
              WHEN floor((c.data_entry_date - to_date(TO_CHAR(a.issue_date, 'DD-MON') || '-1986'))/365) between 21 and 25 then (25*(sum(a.area_in_hectares)))
              WHEN floor((c.data_entry_date - to_date(TO_CHAR(a.issue_date, 'DD-MON') || '-1986'))/365) between 26 and 30 then (30*(sum(a.area_in_hectares)))
              WHEN floor((c.data_entry_date - to_date(TO_CHAR(a.issue_date, 'DD-MON') || '-1986'))/365) between 31 and 35 then (35*(sum(a.area_in_hectares)))
              WHEN floor((c.data_entry_date - to_date(TO_CHAR(a.issue_date, 'DD-MON') || '-1986'))/365) between 36 and 40 then (40*(sum(a.area_in_hectares)))
              WHEN floor((c.data_entry_date - to_date(TO_CHAR(a.issue_date, 'DD-MON') || '-1986'))/365) between 41 and 45 then (45*(sum(a.area_in_hectares)))
              WHEN floor((c.data_entry_date - to_date(TO_CHAR(a.issue_date, 'DD-MON') || '-1986'))/365) between 46 and 50 then (50*(sum(a.area_in_hectares)))
            END
        END)

END AS Revenue
from
tenure a, 
tenure_event_xref b,
event c,
event_type_code d,
title_type_code e
where
a.t_number_id = b.t_number_id
and b.e_number_id = c.e_number_id
and c.event_type_code = d.event_type_code
and a.title_type_code = e.title_type_code

group by TO_CHAR(c.data_entry_date, 'yyyy-mm'), c.event_type_code, d.description, e.description, a.tenure_type_code, a.tenure_sub_type_code

非常感谢!

删除 then 子句中的 sum()。他们case已经是一个sum()的参数。没有必要在 sum() 中执行 sum() -- 这是不允许的。