我有一个合作伙伴为大学的一个小项目做过的SQL查询(这是我们第一次使用SQL),但是我们得到了ora-00907
错误,我们俩都不知道为什么。
我检查过括号并且它们似乎没问题,所以问题必须是另一个。
select
persona.nombre,
anyo,
t2.total
from persona join
(
select
t1.idPersona,
count(produccion.anyo) as total,
anyo
from
(
select *
from produccion
join pelicula
on produccion.id = pelicula.id
) as pel
join
(
select *
from participa
where idPapel = 8
) as t1
on t1.idProduccion = pel.id
)
group by t1.idPersona
) as t2
on persona.id = t2.idPersona
where t2.total > 2
order by t2.total desc;
答案 0 :(得分:0)
您有许多嵌套视图,这使您的查询很难调试。你有很多括号,需要匹配。
无论如何这是错误的:select t1.idPersona, count(produccion.anyo) as total, anyo
。您需要在GROUP BY子句中包含anyo
,这可能会更改您想要的结果集。
select persona.nombre,
t2.anyo,
t2.total
from persona join
(select t1.idPersona,
count(produccion.anyo) as total,
anyo
from (select *
from produccion
join pelicula
on produccion.id=pelicula.id) pel
join
(select *
from participa
where idPapel=8) t1
on t1.idProduccion=pel.id
group by t1.idPersona, t1.anyo) t2
on persona.id=t2.idPersona
where t2.total>2
order by t2.total desc;
答案 1 :(得分:0)
您正在选择*并在创建问题的一列上进行分组。您可以按条件在组下选择相应的列,也可以删除分组。
select *
from (produccion join pelicula on produccion.id=pelicula.id) as pel
join
(select *
from participa
where idPapel=8) as t1
on t1.idProduccion=pel.id)
group by t1.idPersona
以上代码部分不允许使用group by。
如果需要分组,我建议你稍后再使用它。另一种选择是使用分析函数,并在您已经拥有的查询的上层嵌套中过滤掉其他不需要的记录。
答案 2 :(得分:0)
我认为您的查询可以像这样简化/纠正:
select persona.nombre,
anyo,
t2.total
from persona
join (
select par.idPersona,
count(produccion.anyo) as total,
anyo
from produccion
join pelicula
on produccion.id = pelicula.id
left join participa par
on par.idProduccion = pelicula.id -- or produccion.id,
-- this was also an error in the original query,
-- since the subquery selected both
and par.idPapel = 8
group by t1.idPersona
, anyo -- Was missing, but it also doesn't make sense, as this is what you count, so you'll just get 1's here. What do you want with this?
) as t2
on persona.id = t2.idPersona
where t2.total > 2
order by t2.total desc;