计算时间差和两行之间的计算

时间:2016-01-18 11:34:12

标签: mysql date calc

我有一个具有指定id,值,验证和值

的表

MyTable看起来像这样:

+------+------------+---------------------+-------+
| ID   | VALUEID    | VALUEDATE           | VALUE |
+------+------------+---------------------+-------+
| 1    | 2          | 2015-12-21 16:30:00 | 50    |
| 2    | 1          | 2015-12-22 16:33:00 | 54    |
| 3    | 8          | 2015-12-23 16:54:00 | 58    |
| 4    | 6          | 2015-12-24 17:11:00 | 62    |
| 5    | 8          | 2015-12-25 17:11:00 | 66    |
| 6    | 10         | 2015-12-26 10:01:00 | 70    |
| 7    | 7          | 2015-12-27 11:00:00 | 74    |
| 8    | 9          | 2015-12-28 12:03:00 | 78    |
| 9    | 7          | 2015-12-29 12:05:00 | 82    |
| 10   | 6          | 2015-12-30 12:05:00 | 86    |
| 11   | 3          | 2016-01-01 13:19:00 | 90    |
| 12   | 2          | 2016-01-02 13:20:00 | 94    |
| 13   | 7          | 2016-01-03 13:21:00 | 98    |
| 14   | 6          | 2016-01-04 13:51:00 | 102   |
| 15   | 9          | 2016-01-05 13:53:00 | 106   |
| 16   | 3          | 2016-01-06 14:51:00 | 110   |
| 17   | 6          | 2016-01-07 15:31:00 | 114   |
| 18   | 10         | 2016-01-08 15:32:00 | 118   |
| 19   | 7          | 2016-01-09 15:33:00 | 122   |
| 20   | 6          | 2016-01-10 15:34:00 | 126   |
+------+------------+---------------------+-------+

当我想要结果时,valueid = 6.我正在使用此代码。

  

SELECT id,valueid,valuedate,value FROM MyTable WHERE valueid = 6

结果如下所示。

+------+------------+---------------------+-------+
| ID   | VALUEID    | VALUEDATE           | VALUE |
+------+------------+---------------------+-------+
| 4    | 6          | 2015-12-24 17:11:00 | 62    |
| 10   | 6          | 2015-12-30 12:05:00 | 86    |
| 14   | 6          | 2016-01-04 13:51:00 | 102   |
| 17   | 6          | 2016-01-07 15:31:00 | 114   |
| 20   | 6          | 2016-01-10 15:34:00 | 126   |
+------+------------+---------------------+-------+

但我想计算结果。

我想计算前一个条目的天数。此外,应计算一个小的计算。 B值(减号)A值(除数)天。

我试过这样的事,但我没有得到结果。

  

SELECT A.id,A.valueid,A.valuedate,A.value,(B.valuedate -   A.valuedate)AS worthatediff,(B.value - A.value / valuedatediff)AS   valuecalc FROM MyTable INNER JOIN MyTable B ON B.valueid =   (A.valueid + 1)WHERE A.valueid = 6

无论如何,我想要这个结果。

+------+------------+---------------------+-------+---------------+-----------+
| ID   | VALUEID    | VALUEDATE           | VALUE | VALUEDATEDIFF | VALUECALC |
+------+------------+---------------------+-------+---------------+-----------+
| 4    | 6          | 2015-12-24 17:11:00 | 62    | 5             | 4.8       |
| 10   | 6          | 2015-12-30 12:05:00 | 86    | 5             | 3.2       |
| 14   | 6          | 2016-01-04 13:51:00 | 102   | 3             | 4         |
| 17   | 6          | 2016-01-07 15:31:00 | 114   | 3             | 4         |
| 20   | 6          | 2016-01-10 15:34:00 | 126   | 0             | 0         |
+------+------------+---------------------+-------+---------------+-----------+

谢谢!

2 个答案:

答案 0 :(得分:2)

E.g:

SELECT a.id
     , a.valueid
     , a.valuedate
     , a.value
     , b.value - a.value valuediff
     , COALESCE(DATEDIFF(b.valuedate,a.valuedate),0) valuedatediff
     , COALESCE((b.value - a.value)/(DATEDIFF(b.valuedate,a.valuedate)),0) valuecalc
  FROM 
     ( SELECT x.*
            , MIN(y.id) min_id
         FROM my_table x 
         LEFT 
         JOIN my_table y 
           ON y.valueid = x.valueid 
          AND y.id > x.id 
        WHERE x.valueid = 6 
        GROUP 
           BY x.id
     ) a
  LEFT
  JOIN my_table b
    ON b.id = a.min_id;

答案 1 :(得分:0)

对输出的一点修复。

SELECT a.id
     , a.valueid
     , a.valuedate
     , a.value
     , a.value - b.value valuediff
     , DATEDIFF(a.valuedate,b.valuedate) AS valuedatediff
     , (a.value - b.value)/(DATEDIFF(a.valuedate,b.valuedate)) AS valuecalc
  FROM 
     ( SELECT x.*
            , MAX(y.id) max_id
         FROM my_table x 
         LEFT 
         JOIN my_table y 
           ON y.valueid = x.valueid 
          AND y.id < x.id 
        WHERE x.valueid = 6
        GROUP 
           BY x.id
     ) a
  LEFT
  JOIN my_table b
    ON b.id = a.max_id
ORDER BY a.valuedate DESC 

现在,最新的日期首先。

+------+------------+---------------------+-------+-----------+---------------+-----------+
| ID   | VALUEID    | VALUEDATE           | VALUE | VALUEDIFF | VALUEDATEDIFF | VALUECALC |
+------+------------+---------------------+-------+-----------+---------------+-----------+
| 20   | 6          | 2016-01-09 15:34:00 | 126   | 12        | 3             | 4         |
| 17   | 6          | 2016-01-06 15:31:00 | 114   | 12        | 3             | 4         |
| 14   | 6          | 2016-01-03 13:51:00 | 102   | 16        | 4             | 4         |
| 10   | 6          | 2015-12-30 12:05:00 | 86    | 24        | 6             | 4         |
| 4    | 6          | 2015-12-24 17:11:00 | 62    | NULL      | NULL          | NULL      |
+------+------------+---------------------+-------+-----------+---------------+-----------+

希望这是对的:)