Можете да конвертирате avg_aa с bigint или двойна точност. Първо извадете идентификатора и името от полето за име, след което изчислете средната стойност по идентификатор и дата.
-- 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=8113ce4fd19ccd5757>d50ccd286
Използвайте двойна точност в средната колона
-- 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=c788b828008b3ffe3f7e