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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:33:56+00:00 2026-05-14T03:33:56+00:00

Okay, so this one may be difficult but I will do my best to

  • 0

Okay, so this one may be difficult but I will do my best to explain. Bear with me…

I have a table with multiple columns as follows.

ITEM_ID – GROUP_ID
1 – null
2 – null
3 – null
4 – 3
5 – 3
6 – 3
7 – 21
8 – 6
9 – 21
10 – 21
11 – 21
12 – 4
13 – 4
15 – 6
16 – 6
17 – 6
19 – 3

I want to go into this table and return a list such that:

1.) Each item is separate.
2.) Each item is grouped by the group ID.
3.) The result is limited to 5 groups total with each item that is null counted as a single unique group.
4.) When a group is returned, all objects that have the same group number are returned.

With this example I want to return:

1 – null
2 – null
3 – null
4 – 3
5 – 3
6 – 3
19 – 3
7 – 21
9 – 21
10 – 21
11 – 21

So items 1,2 and 3 each count as 1 returned group apiece for a total of 3 returned groups.
Items 4,5,6 and 19 are all group 3 and count as one returned group bringing our total to 4 groups returned. Items 7,9,10 and 11 are all group 21 and count as one returned group bringing our total to 5 returned groups at which point no more groups or single items (which count as groups) are returned.

I am usually pretty good with SQL but this one completely eludes me.

Thanks everyone!!!

  • 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-14T03:33:56+00:00Added an answer on May 14, 2026 at 3:33 am

    This will return what you are asking for. The inner subqueries simply filter the nulls and assign a unique “key” (a negative id number). These are “limited” to the first five unique group ids and are then joined against the a copy of the subquery to return the records we need. The outer subquery is then used to print the result set in the manner reuquested.

    select a.item_id, case when a.group_id <= 0 then null else a.group_id end group_id 
    from (
        select distinct group_id, item_id from test.so_test where group_id is not null 
        union
        (select  -item_id, item_id from test.so_test where group_id is null)
    )  a inner join (
        select distinct group_id from test.so_test where group_id is not null 
        union
        (select  -item_id from test.so_test where group_id is null)
        order by group_id limit 0, 5
    ) b on a.group_id = b.group_id
    order by case when a.group_id <= 0 then null else a.group_id end, a.item_id;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay this one may be a bit out from left field, but I'm going
Okay this may be a simple question but I have yet to come with
Okay, so this is sort of a hack...but it may have to be. I'm
Okay, I have this program and I don't want more than one instance of
Okay, this may be a dumb question, but I've not been able to find
Okay this one has me stumped.. mainly because i have been working on this
Okay, here's what I have... On one server, a WCF hosted in IIS. This
I'm a little new to C#, so bear with me on this one... Okay,
Okay this is a real headscratcher. I have an application which calls a web
Okay I have this RewriteRule which is supposed to redirect any request for the

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.