考虑下表:
sp100_id _date open close bullishness
-----------------------------------------------
1 2011-03-14 100 110 2.23
1 2011-03-15 115 100 1.00
1 2011-03-16 110 110 0.85
2 2011-03-14 90 85 0.99
2 2011-03-15 95 90 0.30
2 2011-03-16 92 100 4.66
3 2011-03-14 200 220 1.50
3 2011-03-15 250 210 1.75
3 2011-03-16 200 150 0.80
我想计算% return
和bullishness
之间的每只股票的2011-03-14
和 ,按DESC2011-03-16
排列它们,然后返回,和。我认为以下查询可以解决问题:% return
sp100_id
% return
average bullishness
SELECT
sp100_id,
AVG(bullishness) as bullishness,
((`close`-`open`) / `open`) as return_pct
FROM
stocks
WHERE _date = BETWEEN '2011-03-14' AND '2011-03-16'
ORDER BY return_pct DESC
但是,它似乎总是只返回一行。然而,预期的输出是:
sp100_id return_pct average bullishness
-----------------------------------------------------------
3 (150-200)/200 = -0.250 (1.50+1.75+0.80)/3 = 4.05
2 (100-90)/90 = 0.110 (0.99+0.30+4.66)/3 = 1.98
1 (110-100)/100 = 0.100 (2.23+1.00+0.85)/3 = 4.08
我究竟做错了什么?