Oracle ORA-00979 - “不是 GROUP BY 表达式”

发布于 2024-10-10 15:33:33 字数 813 浏览 9 评论 0原文

有人可以帮我解决这个特定的查询吗?我在尝试运行此命令时遇到 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

Can anybody please help me with this particular query? I'm having ORA-00979 when trying to run this:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

紫瑟鸿黎 2024-10-17 15:33:33

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

The problem is your subquery with the select count(1) part. Just because it's got a count in it doesn't actually make it an aggregate. It's still a subquery that will be applied to every row and as you can see it uses the value c0.tdata.tariff which is not part of the group.

狼性发作 2024-10-17 15:33:33

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

也许你可以用类似的方法来解决它:

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

Looks like that scalar subquery is causing the problem -- it is neither a group function, nor is it in the GROUP BY list.

Probably you could workaround it with something like:

select   t0.title, count (1) as count0, SUM(select   count (1) ...) AS count1
 ...
も星光 2024-10-17 15:33:33

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

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

Considering this seems to be two instances of the same query just over different dates (I might be wrong here...it's been a long day), you could probably just simplify it and rewrite like this:

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
始终不够爱げ你 2024-10-17 15:33:33

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

Your group by needs to include all of the non-aggregate columns from your select list. In this case, the group by is missing the count1 returned by your subquery.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文