mysql subtract two rows from column and places into an alias

10,063

Just give the table name an alias, and the table inside the correlated subquery a different alias name. Something like this:

SELECT 
  m1.id, 
  m1.kwh,
  COALESCE(m1.kwh - (SELECT m2.kwh 
                     FROM meter_readings AS m2
                     WHERE m2.id = m1.id + 1),
           m1.kwh) AS consumption 
FROM meter_readings AS m1; 

SQL Fiddle Demo

This will give you:

| ID |     KWH | CONSUMPTION |
------------------------------
|  1 | 4567.89 |     1141.18 |
|  2 | 3426.71 |     1181.37 |
|  3 | 2245.34 |     2245.34 |

Update 1

For the updated sample data, just use WHERE m2.id = m1.id - 1 inside the correlated subquery with COALESCE(..., 0) so that the first one will be 0. Like this:

SELECT 
  m1.id, 
  date_format(m1.date_taken, '%Y-%m-%d') AS date_taken,
  m1.kwh,
  COALESCE(m1.kwh - (SELECT m2.kwh 
                     FROM meter_readings m2
                     WHERE m2.id = m1.id - 1), 0) AS consumption 
FROM meter_readings m1; 

Updated SQL Fiddle Demo

This will give you:

| ID | DATE_TAKEN |     KWH | CONSUMPTION |
-------------------------------------------
|  1 | 2013-01-01 | 4567.89 |           0 |
|  2 | 2013-01-08 | 4596.71 |       28.82 |
|  3 | 2013-01-15 | 4607.89 |       11.18 |
Share:
10,063
Paul Lane
Author by

Paul Lane

Every since the zx spectrum I have had a fascination with anything to do with computers. Recently I have been trying to find a Programme language that I enjoy using, to change my job as I know now I should have always gone into the computer / internet world, having a job in that is what I would consider perfecr for me. Started to try and learn Visual basic, but quickly lost interest, then moved onto Pearl enjoyed learning Pearl but couldn't seem to get really enthusiastic about it, so now I have been learning HTML,CSS,PHP,MYSQL,JAVASCRIPT I think I have finally found the language I can really get my teeth into, espically enjoying the database side of it all, and interacting with php.

Updated on June 09, 2022

Comments

  • Paul Lane
    Paul Lane almost 2 years

    I have table meter_readings with columns: id, date_taken, kwh.

    I'm trying to subtract two rows in kwh column together and put the results into an alias called consumption.

    I'm using:

    SELECT id, kwh COALESCE(kwh-(SELECT kwh FROM meter_readings WHERE id= id+1), kwh) AS consumption 
    FROM meter_readings; 
    

    What I get back in consumption alias is simple the same as the original kwh:

     id   date_taken        kwh        consumption 
      1   2013-01-01      4567.89       4567.89 
      2   2013-01-08      4596.71       4596.71  
      3   2013-01-15      4607.89       4607.89
    

    what I would like is:

     id   date_taken        kwh        consumption 
      1   2013-01-01      4567.89          0
      2   2013-01-08      4596.71        28.11
      3   2013-01-15      4607.89        11.18
    

    so id 1 = 0 because this is the first date_taken kwh reading so has no need for a consumption value. This is trying to calculate over a year the weekly kwh consumption.

  • Paul Lane
    Paul Lane about 11 years
    Yep that worked, cheers! I have another column date_taken, is there a way to put the 1st id as 0 as this is the first meter reading taken on a specific date so won't have a consumption value. So the calculation doesn't start till id 2?
  • Mahmoud Gamal
    Mahmoud Gamal about 11 years
    @PaulLane - Sorry, I don't understand, you can use the CASE expression for this: CASE WHEN date_created = ... then ... else ... end Can you please edit your question and add this column date_created with updated sample data. it will be helpful explaining that.
  • Paul Lane
    Paul Lane about 11 years
    Just updated my answer, hopefully makes more sense, thank you.