Average for some records in sql

0
tag         bloque    sector    valor     fecha
temperatura   01   invernadero 19.5   06-09-2018 08:00
temperatura   01   invernadero  20    06-09-2018 08:30
temperatura   02   invernadero  18    06-09-2018 13:30
temperatura   02   invernadero  25    06-09-2018 14:00

I have a database like that, I need to get the average temperature per block.

    
asked by user84003 07.09.2018 в 17:28
source

1 answer

0

Achieve it this way you do a GROUP BY through the block field and apply AVG to the value field:

CREATE TABLE #TABLATEMPORAL (
    tag VARCHAR(20),
    bloque VARCHAR(2),
    sector VARCHAR(20),
    valor DECIMAL(10,2),
    fecha DATETIME
);

INSERT INTO #TABLATEMPORAL VALUES
('temperatura','01','invernadero', 19.5, '2018-09-06 08:00'),
('temperatura','01','invernadero', 20, '2018-09-06 08:30'),
('temperatura','02','invernadero', 18, '2018-09-06 13:30'),
('temperatura','02','invernadero', 25, '2018-09-06 14:00')

SELECT bloque, AVG(valor) AS promedio FROM #TABLATEMPORAL GROUP BY bloque

DROP TABLE #TABLATEMPORAL
    
answered by 08.09.2018 в 17:21