title: How to write a derived query in Netezza SQL?
body: <p>I need to query the data for inviteid based. For each inviteid I need to have the top 5 IDs and ID Descriptions.</p>
  
  <p>I see that the query I wrote is taking all the time in the world to fetch. I didn't notice an error or anything wrong with it. </p>
  
  <p>The code is:</p>
  
  <pre><code>SELECT count(distinct ID),
    IDdesc,
    inviteid,
    A
  FROM (
    SELECT
      ID,
      IDdesc,
      inviteid,
      RANK() OVER(order by  invtypeid asc ) A
    FROM Fact_s
      --WHERE dateid ='26012013'
    GROUP BY invteid,IDdesc,ID
    ORDER BY invteid,IDdesc,ID
  ) B
  WHERE A &lt;=5
  GROUP BY A, IDDESC, inviteid
  ORDER BY A
  </code></pre>