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

标签 mysql sql pivot-table

我想从下表中获取特定加密货币的最新价格(英镑、美元、欧元):

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;

这给了我以下错误:

Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'server.blockchain_currency_price.created' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with 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)

但这些不是最新的条目。

如何在分组时创建上述数据透视表,然后按创建日期排序以仅获取最新条目?

最佳答案

您可以只使用 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;

关于Mysql数据透视表无法按日期排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48475800/

相关文章:

sql - 获取列的最大值和id

sql - 在不存在时尝试删除是行不通的。主键中的多列

database - 如何在 Laravel 中连接 3 个表和一个数据透视表?

java - 如何使用 apache-poi 将数据透视表样式从默认蓝色更改为其他颜色

mysql - 如何在 MySQL 中返回数据透视表输出?

mysql - 选择所有数据包括另一个表,即使为空

php - 如何使 PHP 脚本实时响应数据库插入?

mysql - 计入查询破坏预期结果

mysql - mysql中的错误和警告有什么区别

php - 如何加入 laravel 关系