Return a percentage of the full record set I'm pulling back a list of orders but I need to only display a percentage of the records.  for example we want the top 30 or top 50.  The percentagbe is variable so I'd like to not use TOP.  I tried the code below but I'm getting back no records.

SELECT x.SelectionCriteriaID, 
       x.DeliveryCriteriaID, 
       x.SurveyID, 
       x.intappid, 
       x.intcustomerID, 
       x.intorderid, 
       x.RowNumber RowNumber,
       x.cntoid
  FROM (SELECT @SelectionCriteriaID SelectionCriteriaID, 
               @DeliveryCriteriaID DeliveryCriteriaID, 
               @SurveyID SurveyID, 
               ord.intappid, 
               ord.intcustomerID, 
               ord.intorderid, 
               ord.cntoid,
               ROW_NUMBER() OVER (ORDER BY ord.intcustomerid) AS RowNumber
          FROM ord
         WHERE ord.cntoid &gt;= ISNULL(@MinNumberOrders, 1)
           AND ord.cntoid &lt; ISNULL(@MaxNumberOrders, 999999999))x
 GROUP BY x.SelectionCriteriaID, x.DeliveryCriteriaID, x.SurveyID, x.intappid, x.intcustomerID, x.intorderid, x.RowNumber, x.cntoid
 HAVING x.rownumber &lt; MAX(x.RowNumber)/(100/@percent)
You really should be using the TOP clause.  This is perfectly legal (assuming you're using SQL Server):

declare @top_val int = 30

select top (@top_val) percent
    col1,
    col2,
    col3
from yourTable
order by &lt;something&gt;;


The above query will select the top 30% of the rows.  As far as I can tell from your question, this satisfies your requirements and is the simplest method.