多个汇总作为运行收益

时间:2019-05-07 12:08:50

标签: mysql

我想让价格在一个范围内波动,就像在单个查询中运行均线一样

我有以下代码返回运行最大值,但我也想要最小值

SELECT t1.asatdate, t1.highPrice,
  ( SELECT MAX(t2.highPrice)
    FROM stockdata AS t2
    WHERE DATEDIFF(t1.asatdate, t2.asatdate) BETWEEN 0 AND 4
    and stockcode = 'QBE'
  ) AS '5dayMaxPrice'
FROM stockdata AS t1
where asatdate between '2018-01-01' and  '2018-01-31'
and stockcode = 'QBE'
ORDER BY t1.asatdate;

我尝试了以下方法,但效果很好,但是速度很慢,我觉得必须有一个更简单的解决方案。

SELECT t1.asatdate, t1.highPrice,
  ( SELECT MAX(t2.highPrice)
    FROM stockdata AS t2
    WHERE DATEDIFF(t1.asatdate, t2.asatdate) BETWEEN 0 AND 4
    and stockcode = 'QBE'
  ) AS '5dayMaxPrice',
( SELECT MIN(t2.lowPrice)
    FROM stockdata AS t2
    WHERE DATEDIFF(t1.asatdate, t2.asatdate) BETWEEN 0 AND 4
    and stockcode = 'QBE'
  ) AS '5dayLowPrice'

FROM stockdata AS t1
where asatdate between '2018-01-01' and  '2018-01-31'
and stockcode = 'QBE'
ORDER BY t1.asatdate;

0 个答案:

没有答案