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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:33:09+00:00 2026-06-11T05:33:09+00:00

I have a table, with multiple columns, including a column named PolicyNumber Here’s a

  • 0

I have a table, with multiple columns, including a column named “PolicyNumber”

Here’s a sample:

PolicyNumber

NYH1111
NYD2222
SCH3333
SCS4444
LUH5555
LUS6666
ALH7777
ALW8888
VAH9999
AKH0000
...
NYH1010
NYD2318

There are 1,000+ records in this table and records contain several of each policy number types. For example, multiple policies starting with “NYH” or multiple policies starting with “VAH.”

The possible policy types are here:

NYH
NYD
SCH
SCS
LUH
LUS
ALH
ALW
VAH
AKH

How do I do a SELECT TOP 300 where it’ll INCLUDE at least one of each Policy Type? Remember, a policy type is the first 3 letters of a policy number.

Is this even possible? The purpose of this is that I have to grab 300 records from production to dump into a test environment and I need to include at least 1 of each policy. After I have at least one of each, it can be completely randomized.

  • 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-11T05:33:09+00:00Added an answer on June 11, 2026 at 5:33 am

    You can try this:

    In this solution first there is the newid() with you can generate random order by each running.

    To achive the “at least one from each policy” goal, I made the AtLeastOne column. This selects the first from the randomized CTE table for each unique three letters at the start. If the current Policy equals with this first selected value, then it gets 1 else 0. So with this logic, you can select a randomized first one from each unique three letters.

    Note: You can put this logic directly into the Order By part too if you need the Policy field only. (I made the example on this way to make the logic behind it visible)

    In the last step you just have to order by the AtLeastOne Desc and then by the random ID.

    WITH CTE_Policy
    AS
    (
      SELECT newid() as  ID, Policy
      FROM Code
    )
    SELECT TOP 300
    Policy,
    CASE WHEN Policy = (SELECT TOP 1 Policy FROM cte_Policy c
                        WHERE SUBSTRING(c.Policy,1,3) = 
                          SUBSTRING(CTE_Policy.Policy,1,3))
    THEN 1 ELSE 0 END  AS AtLeastOne
    
    FROM CTE_Policy
    ORDER BY AtLeastOne DESC, ID
    

    Here is an SQLFiddle demo.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have multiple columns in a mySQL table. Three of the columns are named
I have a data table containing multiple columns and one column that stores somewhat
I have a table and this table contain multiple columns. I want to know
I have a table (participants) which has multiple columns that could all be distinct.
I have a table with multiple indexes, several of which duplicate the same columns:
I have table with date/time column and person column. Each person has multiple records
I have a Table (multiple rows, multiple columns, see below ) that is longer
I have a table with multiple columns and I want to update only one
Can SQL table have multiple columns with primary key?
I have a table that contains multiple rows. Each row contains 5 columns (or

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.