avg_aaはbigintまたは倍精度のいずれかに変換できます。最初に名前フィールドからIDと名前を減算し、次にIDと日付で平均を計算します。
-- PostgreSQL(v11)
WITH cte_t AS (
SELECT LEFT(name, 1) id
, RIGHT(name, POSITION('.' IN REVERSE(name)) - 1) t_name
, value
, time_stamp
FROM test
)
SELECT id
, time_stamp :: DATE "date"
, AVG(CASE WHEN t_name = 'aa' THEN value END) :: BIGINT "avg(aa)"
, AVG(CASE WHEN t_name = 'bb' THEN value END) :: BIGINT "avg(bb)"
, AVG(CASE WHEN t_name = 'cc' THEN value END) :: BIGINT "avg(cc)"
FROM cte_t
GROUP BY id, time_stamp :: DATE
ORDER BY "date", id;
URL https://dbfiddle.uk/?rdbms=postgres_11&fiddle=8113ce4fd19d2865から確認してください
平均列で倍精度を使用する
-- PostgreSQL(v11)
WITH cte_t AS (
SELECT LEFT(name, 1) id
, RIGHT(name, POSITION('.' IN REVERSE(name)) - 1) t_name
, value
, time_stamp
FROM test
)
SELECT id
, time_stamp :: DATE "date"
, AVG(CASE WHEN t_name = 'aa' THEN value END) :: DOUBLE PRECISION "avg(aa)"
, AVG(CASE WHEN t_name = 'bb' THEN value END) :: DOUBLE PRECISION "avg(bb)"
, AVG(CASE WHEN t_name = 'cc' THEN value END) :: DOUBLE PRECISION "avg(cc)"
FROM cte_t
GROUP BY id, time_stamp :: DATE
ORDER BY "date", id;
URL https://dbfiddle.uk/?rdbms=postgres_11&fiddle=c70fe828008b13f7eb3fから確認してください