Du kan konvertera avg_aa antingen bigint eller dubbel precision. Subtrahera först id och namn från namnfältet och beräkna sedan genomsnittet efter id och 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;
Vänligen kontrollera från webbadressen https://dbfiddle.uk/?rdbms=911fiddle.uk/?rdbms=911cedgresd5&08111fiddle
Använd dubbel precision i medelkolumnen
-- 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;
Vänligen kontrollera från webbadressen https://dbfiddle.uk/?rdbms=7fiddle.uk/?rdbms=7f8b0208f8b08a&08b3f08b30000000000000000000000000001