2016-10-04 55 views
0

組錯誤我在PostgreSQL的9.4得到一組誤差範圍內:在PostgreSQL中

錯誤:在需要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子句,但是我確實使視圖成爲了一個,並且我有order by子句在那裏,我該如何解決這個錯誤,以及我所做的假設是不正確的?

回答

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); 

其結果是,當您在選擇查詢調用等級,它指的是功能,而不是場(其中做不存在,因爲你已經混淆了它)。