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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:21:17+00:00 2026-06-10T05:21:17+00:00

I have a query that currently returns data with the following attributes: A number

  • 0

I have a query that currently returns data with the following attributes:

  • A number A which is guaranteed to be unique in the result (not in the source table); the result is ordered by A, but the values of A in the result are not necessarily continuous.
  • A key B which is repeated for multiple rows, tagging them as part of the same group. It comes from the same table as A.

Example:

+--+-+-+
|id|A|B|
+--+-+-+
| 5|1|2|
|15|3|2|
|12|4|5|
|66|6|5|
| 2|7|2|
+--+-+-+

I’ve seen answers here which explain how to return the row number in the result. What I do need, however, is to obtain a (preferrably 1-based) order number while keeping a distinct count for each B. In the following table, C is the desired result:

+--+-+-+-+
|id|A|B|C|
+--+-+-+-+
| 5|1|2|1|
|15|3|2|2|
|12|4|5|1|
|66|6|5|2|
| 2|7|2|3|
+--+-+-+-+

This goes a little beyond my current SQL skill, so I’ll be thankful for any pointers. Including pointers to existing answers!

EDIT: Both answers below work equally well in terms of results (with a dummy wrapping query used for sorting). Thank you all for the help. Which would be the most efficient query? Consider that in my specific use case, the amount of rows returned from the original query is never very large (let’s say up to 50 rows, and even that is a stretch of the imagination). Also, the original query has joins used for fetching data from other relations, although they are not relevant for sorting or filtering. Finally, it is possible for all results to have the same B, or for every one of them to have a distinct B – it can go either way or anywhere inbetween.

  • 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-10T05:21:18+00:00Added an answer on June 10, 2026 at 5:21 am

    What you basically want is the RANK() function. However, since it’s not available in MySQL, you can simulate it with:

    SELECT *
    FROM (
      SELECT a, b, (CASE b 
                    WHEN @partition THEN @rank := @rank + 1 
                    ELSE @rank := 1 AND @partition := b END) AS c
      FROM tbl, (SELECT @rank := 0, @partition := '') tmp
      ORDER BY b, a
    ) tmp
    ORDER BY a
    

    DEMO (SQL Fiddle).

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

Sidebar

Related Questions

I currently have a SQL query that returns a number of fields. I need
Dear all, I have a select query that currently produces the following results: DoctorName
I currently have the following SQL query that lists all the names of all
I currently have the following code that retrieves data from the database and then
I have a LINQ query that returns data and I want to filter based
Hey guys I have a query that currently finds the latest comment for each
I currently have a query that looks like this: SELECT NON EMPTY ([Measures].[TOTAL]) ON
I have a query that successfully grabs the unique products from my products table
I have a query that creates a result set like this: Rank Name 1
I currently have a database call which works in one area, but not another.

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.