Search code examples
mysqlsqlcalculated-columns

SQL calculating difference between columns


I'm a bit of a newby at SQL and I don't really understand what to do here, so any help is really appreciated. I have a table full of readings from different readers, there's like 500.000 of them, so I can't do this by hand.

I received the table without the difference in it. I managed to calculate it, but there's a bit of a problem there...

It looks a bit like this:

reader_id |    date    | reading | difference
   1      | 01-01-2013 |   205   |     0
   1      | 02-01-2013 |   210   |     5
   1      | 03-01-2013 |   213   |     3
  ...     |     ...    |   ...   |    ...
   1      | 31-12-2013 |   2451  |     4
   2      | 01-01-2013 |   8543  |    6092
   2      | 02-01-2013 |   8548  |     5

reader_id and date form the primary key. The combination is unique.

How can I make sure I don't get the difference calculated when the last column contained a different reader_id?

When querying my data with a query like this one, the data get skewed by the incorrect difference between the two reader_ids:

SELECT AVG(difference), reader_id FROM table GROUP BY reader_id

Solution

  • For

    I just want to get the average difference for each reader.

    your query is perfectly good. I think you got something wrong in your difference calculation. The first value for reader_id=2, 6092, is the difference of the last reading from reader1 and the first reading from reader 2, i don't think that makes sense. If i'm not mistaken, the difference value is the current day reading - previous day reading. Therefore you should set the difference value of the first reading of each reader to 0.

    You can do this with the following query:

    UPDATE table t INNER JOIN (SELECT reader_id, min(date) as first_day FROM table GROUP BY reader_id) as tmp ON tmp.reader_id=t.reader_id AND tmp.first_day=t.date SET t.difference=0
    

    Then

    SELECT AVG(difference), reader_id FROM table GROUP BY reader_id
    

    will do what you expect.