要计算表中的平均角度(角度单位为度[0, 360]
),我使用以下语句:
SELECT
(CASE WHEN (a < 0.0)
THEN a + 360.0
ELSE a END) as angle
FROM (
SELECT
degrees(atan2(avg(sin(radians(x))), avg(cos(radians(x))))) as a
FROM
angle_t
) as t
UNION
SELECT
x
FROM
angle_t
在进行测试时,我尝试了包含雅虎天气数据的表格:
WITH angle_t(x) AS (
SELECT
cast(wind_direction as double precision)
FROM
weather_yahoo
WHERE
time >= current_date - interval '1 days' - interval '1 hours'
AND
time <= current_date - interval '1 days')
输出是:
246.670436944698
250.0
240.0
我想知道为什么平均角度不是 245 而是 246.67 ......所以我用明显相等的输入数据进行了另一个测试:
WITH angle_t(x) AS (VALUES
(240 :: double precision),
(250))
输出显示了(未)预期的结果:
245.0
250.0
240.0
谁能给我解释一下?(这是 PostgreSQL 8.4)