1

我已经搜索了几个小时,关于如何解决ORA-00937: not a single-group group function并且似乎没有一个可以解释我收到错误的原因。我的理解是,当在一个或多个列上的 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 中的各种列,但看不到它的工作原理。我想知道 CASE 逻辑中的 SUM 聚合是否使用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

非常感激!

4

1 回答 1

1

删除子句sum()中的。then它们case已经是 a 的论点sum()。没有必要在 asum()内做sum()- 这是不允许的。

于 2020-04-23T18:20:34.750 回答