PostgreSQL 14 中窗口范围内的舍入函数
Rounding function in a windowed range in PostgreSQL 14
我找不到一个很好的例子来说明如何内联一个窗口函数。在下面的示例中,我尝试将 Round 函数放在各处(除了正确的位置)。价格是双倍的。如何在 avg 等运算符的窗口结果上内联 Round 函数?
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
您可以将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
我找不到一个很好的例子来说明如何内联一个窗口函数。在下面的示例中,我尝试将 Round 函数放在各处(除了正确的位置)。价格是双倍的。如何在 avg 等运算符的窗口结果上内联 Round 函数?
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
您可以将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