【问题标题】:Oracle ORA-00979 - "not a GROUP BY expression"Oracle ORA-00979 - “不是 GROUP BY 表达式”
【发布时间】:2011-06-04 02:36:17
【问题描述】:

有人可以帮我解决这个特定的问题吗?我在尝试运行时遇到 ORA-00979:

select   t0.title, count (1) as count0, (select   count (1)
      from contract c1, se se1
      where c1.c_id = se1.c_id
      and se1.svc_id = 3
      and se1.deleted = 0
      and c1.deleted = 0
      and c1.c_date between to_date ('07.10.2000', 'dd.mm.yyyy') 
                        and to_date ('22.11.2010', 'dd.mm.yyyy')
      and c1.company = 0
      and c1.tdata.tariff = c0.tdata.tariff
    ) as count1
  from contract c0, se se0, tariff t0
  where c0.c_id = se0.c_id
  and se0.svc_id = 3
  and se0.deleted = 0
  and c0.deleted = 0
  and c0.c_date between to_date ('21.11.2000', 'dd.mm.yyyy') 
                and to_date ('06.01.2011', 'dd.mm.yyyy')
  and c0.company = 0
  and t0.tariff_id = c0.tdata.tariff
  group by t0.title

【问题讨论】:

    标签: sql oracle ora-00979


    【解决方案1】:

    问题是您的子查询带有select count(1) 部分。仅仅因为它有一个计数实际上并不能使它成为一个聚合。它仍然是一个将应用于每一行的子查询,正如您所见,它使用不属于该组的值 c0.tdata.tariff

    【讨论】:

    • 说得好。按 c0.tdata.tariff 分组可以安排事情。
    • @Benoit -- 但是在该附加列上进行分组会改变整体查询结果。而在标量子查询周围添加一个组函数 SUM 可以解决问题并汇总现有组中的计数。
    • 谢谢,这就是这个特定问题的答案。但是这里提出了对整体问题的更好解决方案stackoverflow.com/questions/4605994/…
    【解决方案2】:

    看起来问题是由标量子查询引起的——它既不是组函数,也不在 GROUP BY 列表中。

    也许您可以通过以下方式解决它:

    select   t0.title, count (1) as count0, SUM(select   count (1) ...) AS count1
     ...
    

    【讨论】:

      【解决方案3】:

      考虑到这似乎是同一查询的两个实例,只是在不同的日期(我在这里可能错了......这是漫长的一天),你可能只是简化它并像这样重写:

      select
        t0.title, 
        count (case when c0.c_date between to_date ('21.11.2000', 'dd.mm.yyyy') 
                      and to_date ('06.01.2011', 'dd.mm.yyyy') then 1 end) as count0,
        count (case when c0.c_date between to_date ('07.10.2000', 'dd.mm.yyyy') 
                      and to_date ('22.11.2011', 'dd.mm.yyyy') then 1 end) as count1
      from 
        contract c0, 
        se se0, 
        tariff t0
      where 
        c0.c_id = se0.c_id
        and se0.svc_id = 3
        and se0.deleted = 0
        and c0.deleted = 0
        and (c0.c_date between to_date ('21.11.2000', 'dd.mm.yyyy') 
                      and to_date ('06.01.2011', 'dd.mm.yyyy')
          or c0.c_date between to_date ('07.10.2000', 'dd.mm.yyyy') 
                              and to_date ('22.11.2010', 'dd.mm.yyyy'))
        and c0.company = 0
        and t0.tariff_id = c0.tdata.tariff
      group by t0.title
      

      【讨论】:

      • 这是一个很好的解决方案,谢谢!我不知道这样可以解决问题,它也解决了“零”问题。
      【解决方案4】:

      您的分组依据需要包含您选择列表中的所有非聚合列。在这种情况下,group by 缺少您的子查询返回的 count1

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-29
        • 2013-06-25
        • 2011-04-24
        • 1970-01-01
        相关资源
        最近更新 更多