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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:55:53+00:00 2026-06-18T05:55:53+00:00

This was really difficult to explain in the title of this, but here is

  • 0

This was really difficult to explain in the title of this, but here is a table that I have:

CATEGORY_ID   COUNT   GROUPING
1             130     H
2              54     B
3             128     C
4              70     D
5              31     E
6              25     F
7              64     A
8              59     F
9              66     B
10             62     E
11            129     C
12             52     G
13             27     A
14            102     A
15            101     C

I’m trying to write a query to get the TOP 5 CATEGORY_ID‘s, first ordered by overall COUNT, but then based on that group use the other CATEGORY_ID‘s in that group regardless of their COUNT. So, if I wanted to TOP 5 of the above based on this rule (which I probably explained somewhat poorly), my result would be:

CATEGORY_ID   COUNT   GROUPING
6             25      F <-- THE LOWEST COUNT OVERALL
8             59      F <-- THE NEXT LOWEST IN GROUP 'F'
13            27      A <-- THE NEXT LOWEST OVERALL
7             64      A <-- THE NEXT LOWEST IN GROUP 'A'
14            102     A <-- THE NEXT LOWEST IN GROUP 'A'

I’ve looked a lot here and elsewhere (tried things like RANK(), DENSE_RANK(), GROUPING SETS, and more – mostly as stabs in the dark) and have hit walls all around.

EDIT: One additional thing is that I need to break ties of COUNT randomly. So, for example, if COUNT is 0 for all rows, then the first group returned should be random. I have tried this by adding NEWID() to the ORDER BY in both answers below, but with no luck.

Thank you.

  • 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-18T05:55:55+00:00Added an answer on June 18, 2026 at 5:55 am
    ; with groups as (
      select
        grouping,
        min(count) as group_min
      from categories
      group by grouping
    )
    select top 5 c.category_id, c.count, c.grouping
    from categories c
    join groups g on c.grouping = g.grouping
    order by g.group_min, c.count
    

    Sql Fiddle

    Edit:

    To randomize in case of ties, you can add a random order to each group using row_number() and newid():

    ; with groups as (
      select
        grouping,
        row_number() over (order by newid()) as random,
        min(count) as group_min
      from categories
      group by grouping
    )
    select top 5
      c.category_id, c.count, c.grouping
    from categories c
    join groups g on c.grouping = g.grouping
    order by g.group_min, g.random, c.count
    

    Sql Fiddle

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

Sidebar

Related Questions

This might be a bit difficult to explain but I have two columns in
hey guys having this really simple problem but cant seem to figure out have
This is going to be difficult to explain but hopefully there will be a
Ok this is really difficult to explain in English, so I'll just give an
This is difficult to explain, please bear with me. I have a top menu
It's difficult to explain what I really want. I have an interface which has
I'm having a really difficult time debugging this problem. Whenever I try to establish
This really seems like a bug to me, but perhaps some databinding gurus can
This really puzzled for hours, I searched all over the internet, but got no
I've got this really simple piece of code that I thought was the correct

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.