3

我试图用买入的美元减去卖出的美元。

**|Tran_ID|Currency_type|currency_Amount|Local_Amount|  Rate  |Tran_type| ** 
  | 85     |   USD      |   2500       |    1581.54  |1.58074 |  Bought |
  | 78     |   USD      |   3487.57    |    2206.29  |1.58074 |  Bought |
  | 86     |   USD      |   100        |    63.26    |1.58074 |  Bought |
  | 87     |   USD      |   94.17      |    63.26    |1.48866 |  Sold   |
  | 88     |   USD      |   2600       |    1746.54  |1.48866 |  Sold   |
  | 89     |   USD      |   2600       |    1746.54  |1.48866 |  Sold   |

我可以使用下面的查询添加出售或购买的美元总值

SELECT Currency_type, sum( currency_Amount ) AS total_bought
FROM Transaction
WHERE Currency_type=  'USD'
AND Tran_type =  'Bought'       <==  **or 'Sold'**

结果如下

**|Currency_type | total_bought    |**
|USD             |6087.570068359375|
4

1 回答 1

8

您可以根据您的交易类型设置一个标志:

SUM(currency_Amount * IF(Tran_type = "Bought", 1, -1))
于 2013-04-17T12:00:52.320 回答