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 7890779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:26:42+00:00 2026-06-03T06:26:42+00:00

Let’s assume we have a table with a column ‘A’ that has values from

  • 0

Let’s assume we have a table with a column ‘A’ that has values from 0 to N. And I want to select 30% each rows that have the same value for the column ‘A’.

So if I have this:
A|  B
-------
0 hello
0 test
0 hi
1 blah1
1 blah2
1 blah3
1 blah4
1 blah5
1 blah6

Result:
A|  B
-------
0 hello
1 blah1
1 blah4

it could be blah1 or any other blah that is not blah4, and blah4 can be any other blah that is not blah1, basically it could be random or skipping.

By the way, the actual table is huge, talking terabytes, so think about performance.

  • 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-06-03T06:26:43+00:00Added an answer on June 3, 2026 at 6:26 am

    try something like this:

    DECLARE @YourTable table (A int, b varchar(10))
    INSERT @YourTable VALUES (0, 'hello') --OP's data
    INSERT @YourTable VALUES (0, 'test')
    INSERT @YourTable VALUES (0, 'hi')
    INSERT @YourTable VALUES (1, 'blah1')
    INSERT @YourTable VALUES (1, 'blah2')
    INSERT @YourTable VALUES (1, 'blah3')
    INSERT @YourTable VALUES (1, 'blah4')
    INSERT @YourTable VALUES (1, 'blah5')
    INSERT @YourTable VALUES (1, 'blah6')
    
    ;WITH NumberedRows AS
    (   SELECT 
            A,B,ROW_NUMBER() OVER (PARTITION BY A ORDER BY A,B) AS RowNumber
            FROM @YourTable
    )
    , GroupCounts AS
    (   SELECT
            A,MAX(RowNumber) AS MaxA
            FROM NumberedRows
            GROUP BY A
    )
    SELECT
        n.a,n.b
        FROM NumberedRows           n
            INNER JOIN GroupCounts  c ON n.A=c.A
        WHERE n.RowNUmber<=(c.MaxA+1)*0.3
    

    OUTPUT:

    a           b
    ----------- ----------
    0           hello
    1           blah1
    1           blah2
    
    (3 row(s) affected)
    

    EDIT based on the great idea in the comment from Andriy M

    ;WITH NumberedRows AS
    (   SELECT 
            A,B,ROW_NUMBER() OVER (PARTITION BY A ORDER BY A,B) AS RowNumber
                ,COUNT(*) OVER (PARTITION BY A) AS TotalOf
            FROM @YourTable
    )
    SELECT
        n.a,n.b
        FROM NumberedRows            n
        WHERE n.RowNumber<=(n.TotalOf+1)*0.3
        ORDER BY A
    

    OUTPUT:

    a           b
    ----------- ----------
    0           hello
    1           blah1
    1           blah2
    
    (3 row(s) affected)
    

    EDIT here are “random” rows, using Andriy M idea:

    DECLARE @YourTable table (A int, b varchar(10))
    INSERT @YourTable VALUES (0, 'hello') --OP's data
    INSERT @YourTable VALUES (0, 'test')
    INSERT @YourTable VALUES (0, 'hi')
    INSERT @YourTable VALUES (1, 'blah1')
    INSERT @YourTable VALUES (1, 'blah2')
    INSERT @YourTable VALUES (1, 'blah3')
    INSERT @YourTable VALUES (1, 'blah4')
    INSERT @YourTable VALUES (1, 'blah5')
    INSERT @YourTable VALUES (1, 'blah6')
    
    ;WITH NumberedRows AS
    (   SELECT 
            A,B,ROW_NUMBER() OVER (PARTITION BY A ORDER BY newid()) AS RowNumber
            FROM @YourTable
    )
    , GroupCounts AS (SELECT A,COUNT(A) AS MaxA FROM NumberedRows GROUP BY A)
    SELECT
        n.A,n.B
        FROM NumberedRows           n
            INNER JOIN GroupCounts  c ON n.A=c.A
        WHERE n.RowNUmber<=(c.MaxA+1)*0.3
        ORDER BY n.A
    

    OUTPUT:

    a           b
    ----------- ----------
    0           hi
    1           blah3
    1           blah6
    
    (3 row(s) affected)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say we have a table that has 2 million rows. It has two
Let's say that I have a model that handles recipes, and I want to
Let's say I have table with column 'URL' whrere I store urls like this
Let's say I want to have some kind of a cache that did something
let us assume that I have a reusable business layer that further makes use
Let's say we have 3 classes: A, B and C. Each class has the
Let's assume we have the following structure: index.php config.inc.php \ lib \ lib \
Let's assume that I am at manual A. I press enter and I am
Let's say I have a structure named vertex with a method that adds two
Let me state up front that I have an infantile understanding of Monads. I

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.