我找不到一个很好的例子,说明你是如何在窗口函数周围内联的。在下面的例子中,我尝试将圆形函数放在任何地方(除了正确的位置)。价格是双倍的。如何在窗口的操作符(如avg )的结果上执行内联圆功能?
在这个例子中,nine_day_avg应该四舍五入为两位数。
SELECT quote_date,price,
avg(price)
OVER(ORDER BY quote_date ROWS BETWEEN 8 PRECEDING AND CURRENT ROW) AS nine_day_avg
FROM quote_datas
where symbol = 'A'
order by quote_date desc
发布于 2022-01-22 09:16:31
您可以使用ROUND (source [ , n ] )
作为
source is a number or a numeric expression that is to be rounded
n is an integer that determines the number of decimal places after rounding
注意: n是可选的,如果省略默认值为0。
必须将要舍入为数字的值转换为数字,才能使用上述版本的round
。
SELECT quote_date, price,
round(avg(price::numeric) OVER(ORDER BY quote_date ROWS BETWEEN 8 PRECEDING AND CURRENT ROW), 2) AS nine_day_avg
FROM quote_datas where symbol = 'A' order by quote_date desc
https://stackoverflow.com/questions/70815183
复制