Mysql数据透视表无法按日期排序

时间:2018-01-27 12:27:34

标签: mysql sql pivot-table

我想从下表中获取特定加密货币的最新价格(gbp,usd,eur):

mysql> select * from blockchain_currency_price;
+----+---------------------+----------+------+-------+---------------------+--------------+
| id | blockchain_currency | currency | type | value | created             | created_user |
+----+---------------------+----------+------+-------+---------------------+--------------+
|  1 | ETH                 | USD      | buy  |  1076 | 2018-01-27 10:55:09 | system       |
|  2 | ETH                 | USD      | sell |  1054 | 2018-01-27 10:55:09 | system       |
|  3 | BTC                 | USD      | buy  | 11264 | 2018-01-27 10:55:09 | system       |
|  4 | BTC                 | USD      | sell | 11041 | 2018-01-27 10:55:10 | system       |
|  5 | ETH                 | GBP      | buy  |   760 | 2018-01-27 10:55:10 | system       |
|  6 | ETH                 | GBP      | sell |   745 | 2018-01-27 10:55:11 | system       |
|  7 | BTC                 | GBP      | buy  |  7954 | 2018-01-27 10:55:11 | system       |
|  8 | BTC                 | GBP      | sell |  7797 | 2018-01-27 10:55:12 | system       |
|  9 | ETH                 | EUR      | buy  |   865 | 2018-01-27 10:55:12 | system       |
| 10 | ETH                 | EUR      | sell |   848 | 2018-01-27 10:55:12 | system       |
| 11 | BTC                 | EUR      | buy  |  9062 | 2018-01-27 10:55:13 | system       |
| 12 | BTC                 | EUR      | sell |  8883 | 2018-01-27 10:55:13 | system       |
| 13 | ETH                 | USD      | buy  |  1068 | 2018-01-27 12:18:42 | system       |
| 14 | ETH                 | USD      | sell |  1046 | 2018-01-27 12:18:43 | system       |
| 15 | BTC                 | USD      | buy  | 11077 | 2018-01-27 12:18:43 | system       |
| 16 | BTC                 | USD      | sell | 10857 | 2018-01-27 12:18:44 | system       |
| 17 | ETH                 | GBP      | buy  |   754 | 2018-01-27 12:18:44 | system       |
| 18 | ETH                 | GBP      | sell |   739 | 2018-01-27 12:18:44 | system       |
| 19 | BTC                 | GBP      | buy  |  7822 | 2018-01-27 12:18:45 | system       |
| 20 | BTC                 | GBP      | sell |  7667 | 2018-01-27 12:18:45 | system       |
| 21 | ETH                 | EUR      | buy  |   859 | 2018-01-27 12:18:46 | system       |
| 22 | ETH                 | EUR      | sell |   842 | 2018-01-27 12:18:46 | system       |
| 23 | BTC                 | EUR      | buy  |  8912 | 2018-01-27 12:18:46 | system       |
| 24 | BTC                 | EUR      | sell |  8735 | 2018-01-27 12:18:47 | system       |
+----+---------------------+----------+------+-------+---------------------+--------------+
24 rows in set (0.00 sec)

我尝试使用以下查询执行此操作:

select 
  blockchain_currency BCCurrency,
  max(if(currency='GBP', value, 0)) as GBP,
  max(if(currency='USD', value, 0)) as USD,
  max(if(currency='EUR', value, 0)) as EUR
from blockchain_currency_price
group by blockchain_currency
order by created desc;

这给了我以下错误:

  

ORDER BY子句的表达式#1不在GROUP BY子句中   包含非聚合列   'server.blockchain_currency_price.created',这不是功能上的   依赖于GROUP BY子句中的列;这是不相容的   的sql_mode = only_full_group_by

如果我删除order by条款,我会得到以下内容:

mysql> select 
    ->   blockchain_currency BCCurrency,
    ->   max(if(currency='GBP', value, 0)) as GBP,
    ->   max(if(currency='USD', value, 0)) as USD,
    ->   max(if(currency='EUR', value, 0)) as EUR
    -> from blockchain_currency_price
    -> group by blockchain_currency;
+------------+------+-------+------+
| BCCurrency | GBP  | USD   | EUR  |
+------------+------+-------+------+
| BTC        | 7954 | 11264 | 9062 |
| ETH        |  760 |  1076 |  865 |
+------------+------+-------+------+
2 rows in set (0.01 sec)

但这些不是最近的条目。

如何在分组时创建上述支点,然后按创建日期排序以仅获取最新条目?

1 个答案:

答案 0 :(得分:1)

您可以使用where子句:

    select bcp.*
    from blockchain_currency_price bcp
    where bcp.created = (select max(bcp2.created)
                         from blockchain_currency_price bcp2
                         where bcp2.blockchain_currency = bcp.blockchain_currency and
                               bcp2.currency = bcp.currency
                        );

这会为每个货币对生成一行。如果您确实需要根据blockchain_currency将其旋转一行,则可以使用此where子句进行旋转。

select bcp.blockchain_currency,
         max(case when currency = 'GBP' then value end) as GBP,
         max(case when currency = 'USD' then value end) as USD,
         max(case when currency = 'EUR' then value end) as EUR
from blockchain_currency_price bcp
where bcp.created = (select max(bcp2.created)
                     from blockchain_currency_price bcp2
                     where bcp2.blockchain_currency = bcp.blockchain_currency and
                           bcp2.currency = bcp.currency       
                    )
group by blockchain_currency;