Du kan konvertere avg_aa enten bigint eller dobbelt præcision. Træk først id og navn fra navnefeltet og beregn derefter gennemsnit efter id og dato.
-- PostgreSQL(v11) MED 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 ::DATO "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)" FRA cte_t GROUP BY id, time_stamp ::DATE ORDER BY "date", id;
Tjek venligst fra url https://dbfiddle.uk/?rdbms=911fiddle.uk/?rdbms=911cedfiddle.uk/?rdbms=911cedgresd5&0811postgresfd56000000000000000000
Brug dobbelt præcision i gennemsnitskolonnen
-- 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 ::DATO "date" , AVG(CASE WHEN t_name ='aa' THEN value END) ::DOBBELT PRÆCISION "avg(aa)" , AVG(CASE WHEN t_name ='bb' THEN værdi END) ::DOBBELT PRECISION "avg(bb)" , AVG(CASE WHEN t_name ='cc' THEN value END) ::DOBBELT PRÆCISION "avg(cc)" FRA cte_t GROUP BY id, time_stamp ::DATE ORDER BY "date", id;
Tjek venligst fra url https://dbfiddle.uk/?rdbms=70fe828008a