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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:49:15+00:00 2026-06-02T15:49:15+00:00

This is a question for a SQL specialist! I’m using SQL SERVER 2008 R2

  • 0

This is a question for a SQL specialist!

I’m using SQL SERVER 2008 R2 Express.

I have a table named [myTable] consist of 2 types of records.

The 1st type of records is a master record and the second type is a [Relative] record.

Each master record might have several related record.

I would like to SLELECT TOP 10 * FROM [myTable] master records, and the union sub records like SELECT TOP 4 of [Relative] records for each.

Each record has a [PKID] NO NULL IDENTITY(1,1) PRIMARY KEY CLUSTERED column.

I think I need something like that:

SELECT TOP 10 * FROM [myTable]  WHERE [Relative]=0 
UNION
For each (SELECT TOP 10 [PKID] as Master, * FROM [myTable] WHERE [Relative]=0 )
{SELECT TOP 4 * FROM [myTable] WHERE [Relative] = Master}

How should I correct the query in order to achieve my goal?

I have a secondary question which I doubt there is a simple solution for it:

Relative records comes in two flavors as described by the column [IsImportant].

Is there a way to make sure only 1 important relative record will be selected for each master record?

Is there a way to skip a master record if there are less then 4 relative records for it while only 1 of them is important?

  • 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-02T15:49:17+00:00Added an answer on June 2, 2026 at 3:49 pm

    EDIT

    Is this a correct paraphrase of your requirements?

    [myTable] has 3 relevant fields…

    • PKID : Unique identifier for each row
    • relative : PKID of the row’s parent (0 if the row has no parent [AKA – a master])
    • IsImportant : 0/1 flag

      1. Return no more than 10 master records
      2. For each master record, return no more than 4 relative records
      3. Of the 4 relative records, return no more than 1 IsImportant record
      4. Skip any master that has less than 4 relatives (treating all IsImportant as just 1)

    Questions:
    – If a master has 3 relative records, and none of them are isImportant, still skip?
    – If a master has 4 relative records, and more than 1 is isImportant, still skip?

    Best guess answer…

    WITH
      master_metadata
    AS
    (
      SELECT
        relative         AS MasterID,
        COUNT(*)         AS Relatives,
        SUM(isImportant) AS IsImportantRelatives
      FROM
        [myTable]
      WHERE
        relative <> 0
      GROUP BY
        relative
      HAVING
        COUNT(*) - SUM(isImportant) + MAX(isImportant) >= 4
    )
    ,
      master
    AS
    (
      SELECT TOP 10
        NULL AS sequence_id,
        [myTable].*,
        [master_metadata].Relatives,
        [master_metadata].IsImportantRelatives
      FROM
        [myTable]
      INNER JOIN
        [master_metadata]
          ON [master_metadata].MasterID = [myTable].PKID
      ORDER BY
        [myTable].Selector
    )
    ,
      relative
    AS
    (
      SELECT
        ROW_NUMBER() OVER (PARTITION BY relative, IsImportant ORDER BY Selector)  AS sequence_id,
        *
      FROM
        [myTable]
    )
    ,
      data
    AS
    (
      SELECT
        PKID AS MasterID,
        *
      FROM
        [master]
    
      UNION ALL
    
      SELECT
        [master].PKID AS MasterID,
        [relative].*, Relatives, IsImportantRelatives
      FROM
        [master]
      INNER JOIN
        [relative]
          ON  ([relative].relative = [master].PKID)
          AND (  ([relative].isImportant = 1 AND [relative].sequence_id  = 1)
              OR ([relative].isImportant = 0 AND [relative].sequence_id <= 3)
              OR ([relative].isImportant = 0 AND [relative].sequence_id  = 4 AND [master].IsImportantRelatives = 0)
              )
    )
    
    SELECT
      *
    FROM
      [data]
    ORDER BY
      MasterID,
      CASE WHEN MasterID = PKID THEN 0 ELSE 1 END,
      IsImportant DESC,
      relative
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is for SQL Server 2005. I have a table with 2 columns.
I have this question in which I have a SQL Server Compact Edition database
We are using SQL Server 2005, but this question can be for any RDBMS
Following on from this question: Linq-to-SQL ToDictionary() I have a table called Domains ,
I know this is similar to this question , but I'm using SQL Server
Warm Standby SQL Server/Web Server This question might fall into the IT category but
This question is related to my question: SQL Server and TransactionScope (with MSDTC): Sporadically
This question is for ASP.NET and SQL Server developers. What are your best practices
This question asks about getting a random(ish) sample of records on SQL Server and
This question is pretty much similar to this one , but for SQL Server

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.