我在PostgreSQL 9.4中遇到组内错误:
错误:有序集合排名需要WITHIN GROUP 第4行:(选择a1.rank为r1,
create view a as (select rank() over (order by pid, time)
pid, event, time
from test5);
select e1.prev, e1.event, count(*)
from
eventtransitions as e1,
(select a1.rank as r1,
a2.rank as r2,
a1.event as a1_event,
a2.event as a2_event,
a1.pid as a1_pid,
a2.pid as a2_pid
from a as a1, a as a2) as temp
where r1 = r2-1
and e1.event = a2_event
and e1.prev = a1_event
and a1_pid = a2_pid
group by e1.prev, e1.event;
这对我来说有点奇怪,因为根据我对这个错误的理解,这意味着我需要为我的聚合函数等级设置order by子句。但是,我确实将视图设为a,并且我在那里有了order by子句。如何修复此错误以及我做出的假设是错误的?
答案 0 :(得分:2)
我认为您的错误在视图中是错误的。我相信你想要的是这个:
create view a as
select
rank() over (order by pid, time),
pid, event, time
from test5;
因为它是您当前的视图定义:
create view a as (select rank() over (order by pid, time)
pid, event, time
from test5);
实际上接受rank()
函数并将其别名为“pid”,这相当于:
create view a as (select rank() over (order by pid, time) as pid,
event, time
from test5);
因此,当您在选择查询中调用rank时,它指的是函数,而不是字段(不存在,因为您将其别名化)。