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

  • Home
  • SEARCH
  • 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 6898763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:20:16+00:00 2026-05-27T07:20:16+00:00

I have a panel data set: that is, times , ids , and values

  • 0

I have a panel data set: that is, times, ids, and values. I would like to do a ranking based on value for each date. I can achieve the sort very simply by running:

select * from tbl order by date, value

The issue I have is once the table is sorted in this way, how do I retrieve the row number of each group (that is, for each date I would like there to be a column called ranking that goes from 1 to N).

Example:

Input:

Date, ID, Value
 d1, id1, 2
 d1, id2, 1
 d2, id1, 10
 d2, id2, 11

Output:

Date, ID, Value, Rank
 d1, id2, 1, 1
 d1, id1, 2, 2
 d2, id1, 10, 1
 d2, id2, 11, 2
  • 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-27T07:20:16+00:00Added an answer on May 27, 2026 at 7:20 am

    Absent window functions, you can order tbl and use user variables to compute rank over your partitions (“date” values) yourself:

    SELECT "date",                                                -- D) Desired columns
           id,
           value,
           rank
      FROM (SELECT "date",                                        -- C) Rank by date
                   id,
                   value,
                   CASE COALESCE(@partition, "date")
                     WHEN "date" THEN @rank := @rank + 1
                     ELSE             @rank := 1
                   END AS rank,
                   @partition := "date" AS dummy
              FROM (SELECT @rank := 0 AS rank,                    -- A) User var init
                           @partition := NULL AS partition) dummy
                   STRAIGHT_JOIN
                   (  SELECT "date",                              -- B) Ordering query
                             id,
                             value
                        FROM tbl
                    ORDER BY date, value) tbl_ordered;
    

    Update

    So, what is that query doing?

    We are using user variables to “loop” through a sorted result set, incrementing or resetting a counter (@rank) depending upon which contiguous segment of the result set (tracked in @partition) we’re in.

    In query A we initialize two user variables. In query B we get the records of your table in the order we need: first by date and then by value. A and B together make a derived table, tbl_ordered, that looks something like this:

    rank | partition | "date" |  id  | value 
    ---- + --------- + ------ + ---- + -----
      0  |   NULL    |   d1   |  id2 |    1
      0  |   NULL    |   d1   |  id1 |    2
      0  |   NULL    |   d2   |  id1 |   10
      0  |   NULL    |   d2   |  id2 |   11
    

    Remember, we don’t really care about the columns dummy.rank and dummy.partition — they’re just accidents of how we initialize the variables @rank and @partition.

    In query C we loop through the derived table’s records. What we’re doing is more-or-less what the following pseudocode does:

    rank      = 0
    partition = nil
    
    foreach row in fetch_rows(sorted_query):
      (date, id, value) = row
    
      if partition is nil or partition == date:
        rank += 1
      else:
        rank = 1
    
      partition = date
    
      stdout.write(date, id, value, rank, partition)
    

    Finally, query D projects all columns from C except for the column holding @partition (which we named dummy and do not need to display).

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

Sidebar

Related Questions

I have an unbalanced quarterly panel data set with missing values. I want to
I would like to create 10 plots that have different data, but the same
I have a panel data set in R (time and cross section) and would
I'd like to have both the values and the data categories on a graph.
I have and Update Panel with a Grid inside of it. The grid's data
I have panel that is using group layout to organize some label. I want
I have an Panel control that I need to maintain position across postbacks. I
I have a panel that draws alot of things. To make drawing effecient I
i have a panel (A), which contains 3 other panels (AA, AB, AC). Each
I have update panel that content check box, textbox, 3 DropDownList with CascadingDropDown extender.

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.