如果不是SQL

时间:2014-03-11 23:55:24

标签: mysql

我有一张etrade交易表。我想

SELECT symbol, sum(shares), (price * shares) as cost, sum(total) as total
FROM transactions_orig
WHERE type = 'EQ' AND IF(transaction = 'dividend', shares <> 0, ??)
GROUP BY symbol
HAVING sum(shares) > 0

所以我只想在交易类型为股息并且股票不等于0时包括该行。这意味着股息被再投资而不是现金支付。 如果交易等于股息且股票等于零,则忽略该行。

我确定我在想这个......但我无法弄明白。以下是一些示例数据

1. Dividend   EQ        LGCY        0        114.00   0.00
2. Credit     UNKNOWN               0          9.99   0.00
3. Bought     EQ        BBEP      100      -1978.99  19.69
4. Dividend   EQ        T           0        157.50   0.00
5. Dividend   EQ        T           4.42686 -157.50   0.00
6. Adjustment UNKNOWN               0        -90.00   0.00

我试图排除的是第4行(支付股息)等行,但包括第5行(购买更多股票)

2 个答案:

答案 0 :(得分:1)

据我了解你的要求。您需要选择所有交易,但不包括零股的股息。

格式化where子句
WHERE type = 'EQ' AND IF(transaction = 'dividend', shares <> 0, ??)

WHERE type = 'EQ' AND ((transaction = 'dividend' and shares <> 0) OR transaction <> 'dividend')

答案 1 :(得分:0)

这样的东西?

SELECT symbol, sum(shares), (price * shares) as cost, sum(total) as total
FROM transactions_orig
WHERE type = 'EQ' AND transaction = 'dividend' and shares <> 0
GROUP BY symbol
HAVING sum(shares) > 0