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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:01:36+00:00 2026-06-09T22:01:36+00:00

I would like help with mysql query that refers to the cell above. for

  • 0

I would like help with mysql query that refers to the cell above.
for example, in the following table:

primary key(id)     day             count           percentage change
1                  monday             1               0  
2                  tuesday            2              (1-0)*100%=100%  
3                  wednesday          5              (2-1)*100%=100%  
4                  thursday           9              (5-2)*100%=300%  
5                  friday             27             (9-5)*100%=400%  

The percentage change results are based on the results of the previous two days of the count column. Is there a way to incorporate the primary key(id) to refer to the cells “above”?

  • 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-09T22:01:37+00:00Added an answer on June 9, 2026 at 10:01 pm

    There is not an easy way. You need to use a self join.

    select t.*,
           (coalesce(t_1.count, 0) - coalesce(t_2.count, 0)) * 100.0
    from t left outer join
         t t_1
         on t.id = t_1.id + 1 left outer join
         t t_2
         on t.id = t_2.id + 2
    

    The left outer join makes sure all the original rows stay, even when there are not preceding ids.

    This works because the ids are sequential. If the ids are not sequential, and the counts are monotonically increasing, you can do this with correlated subqueries:

    select t.*,
           (coalesce((select max(`count`) as val
                      from table1 t_1
                      where t_1.`count` < t.`count`
                     ), 0)
           ) -
           (coalesce((select max(`count`)
                      from table1 t_2
                      where t_2.`count` < (select max(`count`) from table1 t_1 where t_1.`count` < t.`count`)
                     ), 0)
           )
    from table1 t
    

    Note: this will not work correctly if two values are the same in a row. For that, you would need to use the id instead:

    select t.*,
           (coalesce((select max(`count`) as val
                      from table1 t_1
                      where t_1.`id` < t.`id`
                     ), 0)
           ) -
           (coalesce((select max(`count`)
                      from table1 t_2
                      where t_2.`id` < (select max(`id`) from table1 t_1 where t_1.`id` < t.`id`)
                     ), 0)
           )
    from table1 t
    

    If the counts are not increasing, then you have to get the ids, and join in the values again. What fun! Here is the code:

    select t.*,
           coalesce(t1.count, 0) - coalesce(t2.count, 0)
    from (select t.*,
                 (select max(`id`) as id1 from table1 t_1 where t_1.`id` < t.`id`
                 ) as id1,
                 (select max(`count`) from table1 t_2
                  where t_2.`id` < (select max(`id`) from table1 t_1 where t_1.`id` < t.`id`)
                 ) id2
          from table1 t
         ) t left outer join
         table1 t1
         on t.id1 = t1.id left outer join
         table1 t2
         on t.id2 = t2.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL query that I'd like help optimizing. Its rather simple in
I would like to know how I can query MySQL so that entries that
I'm still fairly new to mysql and would like to ask for some help.
I am trying to write a query and would like some help if possible.
i would like a simple help... i have a url like this: example.com/profile.php?id= &
i would like you to help me. I want to create dynamicly table (screenshot).
I would like to construct a query that fetches results that occurred between NOW
I would like to be able to add a mysql query to my Javascript
I'm really struggling with a MySQL query that I'm really hoping someone can help
In MySql, Let's say I have a table myTable ID word I would like

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.