Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 913345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:32:32+00:00 2026-05-15T17:32:32+00:00

Question How do I write a T-SQL Stored Procedure that lets me select percentages

  • 0

Question

  • How do I write a T-SQL Stored Procedure that lets me select percentages of rows between X% and Y%?
  • So basically I would want to select the rows between 30 PERCENT and 40 PERCENT…..

I know that you can do the following, but obviously that doesn’t let met specify a set of rows between 2 percentages.

SELECT TOP 50 PERCENT * FROM tblAssets 

Help greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T17:32:33+00:00Added an answer on May 15, 2026 at 5:32 pm

    Updated Answer

    declare @NumRecords int
    SELECT @NumRecords = COUNT(*) FROM tblAssets;
    
    With Vals As
    (
    SELECT tblAssets.AssetId ...
    , ROW_NUMBER() OVER ( order by tblAssets.AssetId) as RN
      FROM tblAssets
    )
    
    SELECT  tblAssets.AssetId ...
    FROM vals 
    Where RN between 0.3*@NumRecords and 0.4*@NumRecords
    

    I’ve updated my answer as there were 2 problems with my original answer below

    1. Performance – It was beaten by the nested TOP solution
    2. Accuracy – There is an unexpected aspect of NTILE that I was not aware of

    If the number of rows in a partition
    is not divisible by
    integer_expression, this will cause
    groups of two sizes that differ by one
    member. Larger groups come before
    smaller groups in the order specified
    by the OVER clause. For example if the
    total number of rows is 53 and the
    number of groups is five, the first
    three groups will have 11 rows and the
    two remaining groups will have 10 rows
    each.

    I got the following values comparing with the nested TOP solution.

    SET STATISTICS IO ON
    SET STATISTICS TIME ON;
    
    DECLARE @NumRecords int
    SELECT @NumRecords = COUNT(*) FROM [master].[dbo].[spt_values];
    
    WITH Vals As
    (
    SELECT  [number]
    , ROW_NUMBER() OVER ( order by [number]) as RN
      FROM [master].[dbo].[spt_values]
    )
    
    SELECT [number] FROM vals Where RN
     BETWEEN 0.30*@NumRecords AND 0.40*@NumRecords
    

    Gives

    Table ‘spt_values’. Scan count 1,
    logical reads 8, physical reads 0,
    read-ahead reads 0, lob logical reads
    0, lob physical reads 0, lob
    read-ahead reads 0.

    Table
    ‘spt_values’. Scan count 1, logical
    reads 5, physical reads 0, read-ahead
    reads 0, lob logical reads 0, lob
    physical reads 0, lob read-ahead reads
    0.

    SELECT TOP 25 PERCENT [number] FROM
    (
    SELECT TOP 40 PERCENT  [number]
    FROM  [master].[dbo].[spt_values]
    ORDER BY [number]  ASC
    ) TOP40
    ORDER BY [number] DESC
    

    Gives

    Table ‘Worktable’. Scan count 1,
    logical reads 4726, physical reads 0,
    read-ahead reads 0, lob logical reads
    0, lob physical reads 0, lob
    read-ahead reads 0.

    Table
    ‘spt_values’. Scan count 1, logical
    reads 8, physical reads 0, read-ahead
    reads 0, lob logical reads 0, lob
    physical reads 0, lob read-ahead reads
    0.

    Original Answer

    With Vals As
    (
    SELECT tblAssets.AssetId ...
    , NTILE (100)  OVER ( order by tblAssets.AssetId) as Pct
      FROM tblAssets 
    )
    
    SELECT * FROM vals Where Pct between 30 and 40
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.