有人可以帮我解决这个问题吗?我在尝试运行时遇到了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
答案 0 :(得分:3)
问题是您的子查询select count(1)
部分。仅仅因为它有一个计数它实际上并没有使它成为一个聚合。它仍然是一个子查询,将应用于每一行,您可以看到它使用的值c0.tdata.tariff
不属于该组。
答案 1 :(得分:1)
看起来标量子查询导致了问题 - 它既不是组函数,也不在GROUP BY列表中。
可能你可以通过以下方式解决问题:
select t0.title, count (1) as count0, SUM(select count (1) ...) AS count1
...
答案 2 :(得分: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
答案 3 :(得分:0)
您的群组需要包含选择列表中的所有非汇总列。在这种情况下,group by缺少子查询返回的count1
。