2

有人可以帮我解决这个特定的问题吗?我在尝试运行时遇到 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
4

4 回答 4

3

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

于 2011-01-05T15:54:34.573 回答
1

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

可能您可以使用以下方法解决它:

select   t0.title, count (1) as count0, SUM(select   count (1) ...) AS count1
 ...
于 2011-01-05T15:55:00.503 回答
1

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

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
于 2011-01-05T19:54:36.383 回答
0

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

于 2011-01-05T15:54:12.207 回答