title: Within Group Error in Postgresql
body: <p>I am getting a within group error in PostgreSQL 9.4:</p>
  
  <p>ERROR:  WITHIN GROUP is required for ordered-set aggregate rank
  LINE 4:  (select a1.rank as r1,</p>
  
  <pre><code>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;
  </code></pre>
  
  <p>Which is a little weird to me because from what I understand about this error, it means that I need to have order by clause for my aggregate function rank. However, I did make the view a and I have order by clause there. How do I fix this error and what assumption am I making that is incorrect?</p>