计算过去两天(最多两天)之间的百分比变化?

时间:2017-07-15 11:20:49

标签: mysql sql

我试图从mysql表中获取最多两天,以便使用收盘价来计算更改的百分比。我想获得最近两天的收盘价。

我有这样的数据,

businessdate| close       | volume | stock_symbol
2014-03-25  |    4.58     |   899  |  AP
2014-03-24  |    3.57     |   345  |  AC
2014-03-21  |    3.61     |   355  |  AT

期待输出

businessdate| close  | % Change
2014-03-25  |    4.58|   1.01

我使用了以下查询,

SELECT x.businessdate, x.close,x.volume,x.stock_symbol (((x.close / y.close) - 1) * 100) AS "% Change"
FROM (SELECT a.businessdate AS abusinessdate, MAX(b.businessdate) AS aPrevDate
      FROM stockdaily a INNER JOIN
           stockdaily b
      WHERE a.businessdate > b.businessdate AND
            stock_symbol in ('AAPL', 'FB')
      GROUP BY a.businessdate
     ) Sub1 INNER JOIN
     stockdaily x
     ON Sub1.abusinessdate = x.businessdate INNER JOIN
     stockdaily y
     ON Sub1.aPrevDate = y.businessdate
ORDER BY x.businessdate DESC

0 个答案:

没有答案