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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:31:21+00:00 2026-05-25T09:31:21+00:00

I wanted to get the latest 4 dates for each symbolid . I adapted

  • 0

I wanted to get the latest 4 dates for each symbolid. I adapted the code here as follows:

set @num := 0, @symbolid := '';
select symbolid, date,
@num := if(@symbolid = symbolid, @num + 1, 1) as row_number,
@symbolid := symbolid as dummy
from projections
group by symbolid, date desc
having row_number < 5

and get the following results:

symbolid                date   row_number   dummy       
1       '2011-09-01 00:00:00'       1       1
1       '2011-08-31 00:00:00'       3       1
1       '2011-08-30 00:00:00'       5       1
2       '2011-09-01 00:00:00'       1       2
2       '2011-08-31 00:00:00'       3       2
2       '2011-08-30 00:00:00'       5       2
3       '2011-09-01 00:00:00'       1       3
3       '2011-08-31 00:00:00'       3       3
3       '2011-08-30 00:00:00'       5       3
4       '2011-09-01 00:00:00'       1       4
...

The obvious question is, why did I only get 3 rows per symbolid, and why are they numbered 1,3,5? A few details:

  1. I tried both forcing an index and not (as seen here), and got the same results both ways.
  2. The dates are correct, i.e., the listing correctly shows the top 3 dates per symbolid, but the row_number value is off
  3. When I don’t use the “having” statement, the row numbers are correct, i.e., the most recent date is 1, the next most recent is 2, etc

Obviously the row_number computed field is being affected by the “having” clause, but I don’t know how to fix it.

I realize that I could just change the “having” to “having row_number < 7” (6 gives the same as 5), but it’s very ugly and would like to know what to do to make it “behave”.

  • 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-25T09:31:21+00:00Added an answer on May 25, 2026 at 9:31 am

    I’m not 100% sure why it behaves this way (maybe it’s because logically SELECT is processed prior to ORDER BY), but it should work as expected:

    SELECT * 
    FROM 
    (
        select symbolid, date,
        @num := if(@symbolid = symbolid, @num + 1, 1) as row_number,
        @symbolid := symbolid as dummy
        from projections
        INNER JOIN (SELECT @symbolid:=0)c
        INNER JOIN (SELECT @num:=0)d
        group by symbolid, date desc
    
    ) a
    WHERE row_number < 5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the latest code. I wanted to try something different but I am
I wanted to get the keyCode on keydown event in ckeditor. My code looks
When I wanted to get Android source code, I knew that I have to
Well I wanted to know if I could get the latest tag from subversion,
I wanted to get the words from a cell. For example, cell A2 has
I wanted to get some feedback on your thoughts of storing entire objects into
If i wanted to get message for ViewData from resource file, depending on the
I just wanted to get the general consensus on which is a better mode
I am wanted to get the id's of all the divs on my page
I'm new to objective C and I just wanted to get a general clarification

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.