U kunt avg_aa converteren naar bigint of dubbele precisie. Trek eerst id en naam af van het naamveld en bereken vervolgens het gemiddelde op id en datum.
-- 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;
Controleer a.u.b. vanaf url https://dbfiddle.uk/?rdbms=postgres_114&fid57d2813ce65
Dubbele precisie gebruiken in kolom Gem
-- 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;
Controleer a.u.b. vanaf url https://dbfiddle.uk/?rdbms=postgres_11828008b1370fe7828008b1370fe7828008b1370fe7828008b1370fe7828008b1370fe7828008b1370fe7828008b1370fe7828008=c70fe828008