获取最后记录的百分比差异

时间:2014-01-21 23:23:49

标签: mysql sql

我有下一张桌子:

+----+-----------+------------+-----------+
| ID | market_id | last_value | timestamp |
+----+-----------+------------+-----------+
| 1  |    1      |     10     |   1001    |
| 2  |    1      |     15     |   1002    |
| 3  |    1      |     25     |   1003    |
| 4  |    1      |     30     |   1004    |
| 5  |    1      |     05     |   1005    |
+----+-----------+------------+-----------+

我的想法与此类似:

SELECT id, market_id, last_value, timestamp from data_markets
where from_unixtime(timestamp, '%d-%m-%Y %H:%m:%s') >= DATE_FORMAT(NOW(),'%d-%m-%Y 00:00:00') and market_id = 1

有了这个,我得到今天的所有行,但我知道使用join来得到这个:

+---+----------------------+--------------------+----------+
| market_id |    change    |    total_change    |  is_down |
+-----------+--------------+--------------------+----------+
|     1     |     83.3%    |      -50.00%       |   true   |
+-----------+--------------+--------------------+----------+

说明:

  • 更改:此值是与最后一行的差异

  • Total_change:此值是“24小时”中所有行的差异

  • 是关闭的:可能知道值是在增加还是减少总量变化?

编辑:

我编辑了这个例子,在total_change中我喜欢与所有行的区别,但这不可能与第一行有所不同。

1 个答案:

答案 0 :(得分:1)

这是一个我认为可以为您提供所需内容的查询。

SELECT
    dm.id,
    dm.market_id,
    dm.last_value,
    (@prev_value := (SELECT last_value FROM data_markets WHERE (id < dm.id AND market_id = dm.market_id) ORDER BY id DESC LIMIT 1)) AS prev_value,
    ROUND((@24hr_value := (SELECT AVG(last_value) FROM data_markets WHERE (timestamp >= (dm.timestamp - 86400) AND timestamp < dm.timestamp AND market_id = dm.market_id))),1) AS 24hr_value,
    ROUND(IF(@prev_value,(((dm.last_value - @prev_value) / @prev_value) * 100),0),1) AS percent_change,
    ROUND((@24hr_change := IF(@24hr_value,(((dm.last_value - @24hr_value) / @24hr_value) * 100),0)),1) AS 24hr_change,
    IF(@24hr_change < 0, 'down', IF(@24hr_change > 0, 'up', '-')) AS direction
FROM
    data_markets dm,
    (SELECT
        @prev_value:=0,
        @24hr_value:=0,
        @24hr_change:=0
    ) r
WHERE
    dm.market_id = 1
ORDER BY
    dm.id ASC

您没有解释订单应如何确定行的“之前”值。我假设您希望通过混合使用先前的数字id值来确定这一点,同时确保market_id是相同的。让我知道,如果我误解了,那就是别的了。然后,我计算最后一个值的百分比变化,以及过去24小时内AVERAGE最后一个值的变化百分比。最后,24小时百分比确定“方向”列。这是输入数据,稍微调整了您提供的内容:

===========================================
|id | market_id | last_value | timestamp  |
===========================================
|1  | 1         | 10         | 1290347095 |
|2  | 1         | 15         | 1370347095 |
|3  | 1         | 25         | 1390345341 |
|4  | 1         | 30         | 1390346043 |
|5  | 1         | 5          | 1390347095 |
===========================================

最终结果集:

===============================================================================
|id | market_id | last_value | prev | 24hr | change | 24hr_change | direction |
===============================================================================
| 1 | 1         | 10         | NULL | NULL | 0.0    | 0.0         | -         |
| 2 | 1         | 15         | 10   | NULL | 50.0   | 0.0         | -         |
| 3 | 1         | 25         | 15   | NULL | 66.7   |  0.0        | -         |
| 4 | 1         | 30         | 25   | 25.0 | 20.0   | 20.0        | up        |
| 5 | 1         | 5          | 30   | 27.5 | -83.3  | -83.6       | down      |
===============================================================================