Sunday, 15 January 2012

Get difference between values in MySQL -


I have a table with the following columns

  id pl_date month score that ID 1 20015 / 02/04 02 9 244 2 20015/02/05 02 12 244 3 20015/02/08 02 8 244 4 20015/02/22 02 24 244 5 20015/03/10 03 10 244 6 20015/03/11 03 12 244 7/2005/03/12 03 10 244 8 20015/03/13 03 12 244  

My goal score with a specific SELECT with mysql

 Calculate the difference between I  id pl_date month score kid difference 1 2015/02/04 02 9 244 -3 2 2015/02/05 02 12 244 +4 3 2015/02/08 02 8 244 -16 4 2015-02-22 22 24 244 +14 5 2015-03-03 03 10 244 -2 6 2015/03/11 03 12 244 +2 7 2015-03-03 03 10 244 -2 8 2015/03/13 0 3 12 244 12  

I got down Eaten by trying to use the subkey, but it does not work.

  SELECT b.id, (b.max_count -bmIQQ) from AS DIFF (SELECT id, MAX (score) as MaxCount, MIN (score) MYTable WHERE months = 03 by min_count and KID = 244 by the group KID) 
< P> You can join the table with yourself (one yourself involved), but with the transfer of the ID to one:
  select t1. *, Ifnull (t1.scores - t2.scores, t1.scores) table 1 t1 left as a diff, t1.id + 1 = t2.id on table 1 t2  

If you want to calculate the difference in only a few groups (such as IDs), just add those conditions by adding them.

Another approach is a correlated subquery (this uses the date rather than the id to determine the successor):

  select t1. *, T1.scores - ifnull ((Select table where pl_dat E & gt; T1.pl_date limit1), 0) ID by table 1 as T1 order diff;  

(both views)


No comments:

Post a Comment