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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:18:48+00:00 2026-06-13T00:18:48+00:00

I have a table with IDs, rank, chart_date, and pageviews. It’s based on a

  • 0

I have a table with IDs, rank, chart_date, and pageviews. It’s based on a cron job that is run nightly and compiles the number of pageviews for that ID.

For instance:

ID   |   RANK   |   PAGEVIEWS   |  CHART_DATE
5          1           100         2012-10-14
9          2           75          2012-10-14
13         3           25          2012-10-14
9          1           123         2012-10-13
5          2           74          2012-10-13
19         3           13          2012-10-13

So I’m grabbing today’s chart based on 2012-10-14 and ranking the data by 1-3. But I also want to show the rank where the ID was on the previous date.

For instance, on 2012-10-14 ID 5 was ranked 1 but on 2012-10-13 it was ranked 2.

Can I do this with one query? Or do I have to loop thru the results based on today and do a query for each ID?

  • 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-13T00:18:49+00:00Added an answer on June 13, 2026 at 12:18 am

    Can I do this with one query?

    You can, but you need a JOIN between the table with today’s date and the table with yesterday’s date:

    SELECT today.*, yesterday.rank
        FROM yourtable AS today
        JOIN yourtable AS yesterday
            ON (today.id = yesterday.id
                AND today.chart_date = date(now())
                AND yesterday.chart_date = date(date_sub(now(), interval 1 day))
            )
        ORDER BY today.rank DESC;
    

    You can even show the difference:

    SELECT today.*, yesterday.rank AS yest, yesterday.rank-today.rank AS incr
        FROM yourtable AS today
        LEFT JOIN yourtable AS yesterday
            ON (today.id = yesterday.id
                AND today.chart_date = date(now())
                AND yesterday.chart_date = date(date_sub(now(), interval 1 day))
            )
        ORDER BY today.rank DESC;
    
    ID   |   RANK   |   PAGEVIEWS   |  CHART_DATE  |  YEST  |  INCR
    5          1           100         2012-10-14     2     |   1
    9          2           75          2012-10-14     1     |  -1
    13         3           25          2012-10-14     4     |   1
    

    (LEFT JOIN ensures today’s data is there even if yesterday’s isn’t).

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

Sidebar

Related Questions

I have a table of userdata that includes user ID and category IDs. I
Hi I have a table that has trs and IDs embedded to it. My
I have a table that has IDs and Strings and I need to be
I have a table of user ids that is huge. I have an array
I have a table without IDs. it has 3 columns: the name of a
I have a table which contains User IDs, Period IDs and Qty: User Period
I have a log table containing user ids, ip addresses and log in datetimes.
I have a table in a div , with IDs of table1 and div1
I have a table, called report, which contains report IDs and their respective extension
I have a main table called M1 which consists of Ids of three individual

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.