如果是,那么我们可以实现这一点吗?

时间:2018-03-06 13:28:26

标签: mysql sql database

我有一张包含以下类型数据的表格:

+---------+------+--------+
| market  | type | amount |
+---------+------+--------+
| LTCBTC  | SELL |   0.15 |
| XMRBTC  | BUY  |  0.005 |
| DASHBTC | BUY  |  0.002 |
| ADABTC  | BUY  |      7 |
| IOTABTC | BUY  |      1 |
| LTCBTC  | BUY  |   0.17 |
| XRPBTC  | BUY  |     12 |
| ETHBTC  | BUY  |   0.03 |
| BCCBTC  | BUY  |  0.005 |
| ETHBTC  | SELL |  0.124 |
| LTCBTC  | BUY  |   0.02 |
| XMRBTC  | BUY  |  0.004 |
| DASHBTC | BUY  |  0.002 |
| ADABTC  | BUY  |      6 |
| IOTABTC | BUY  |      1 |
| XRPBTC  | BUY  |     10 |
| BCCBTC  | BUY  |  0.004 |
| ETHBTC  | BUY  |  0.025 |
| XMRBTC  | BUY  |  0.006 |
| DASHBTC | BUY  |  0.002 |
| LTCBTC  | BUY  |   0.02 |
| ETHBTC  | SELL |  0.026 |
| ETHBTC  | SELL |  0.004 |
| BCCBTC  | SELL |  0.005 |
| XRPBTC  | SELL |     14 |
+---------+------+--------+

这是我直到今天买或卖的硬币对的信息。我想找到我为每对配对的确切数量的硬币。

所以基本上我想要以下回复:

+---------+------+----------------------+
| market  | type | sum(amount)          |
+---------+------+----------------------+
| ADABTC  | BUY  |                   13 |
| BCCBTC  | BUY  |        0.00400000019 |
| DASHBTC | BUY  | 0.006000000284984708 |
| ETHBTC  | BUY  |         -0.098999999 |
| IOTABTC | BUY  |                    2 |
| LTCBTC  | BUY  |         0.0599999949 |
| XMRBTC  | BUY  |  0.01500000013038516 |
| XRPBTC  | BUY  |                    8 |
+---------+------+----------------------+

有人可以帮我查询相同的内容吗?

由于

1 个答案:

答案 0 :(得分:1)

我想简单的CASEGROUP BY应该有用

select market,
       sum(case when type = 'sell' then -amount
                when type = 'buy' then amount end)
from data
group by market