简体   繁体   中英

Average by day SQL

I need to have a table with the average by hour or by day of a table. The problem is the average is made by hour/min/sec

Here's the query: 在此处输入图像描述

For SQL you need to CONVERT the date

SELECT CONVERT(VARCHAR(10), DATE_PEREMPTION, 112) AS TIME_PERIOD, AVG(MESURE) AS MESURE
...
GROUP BY TIME_PERIOD
ORDER BY TIME_PERIOD

This might be a useful reference : https://www.sqlservertutorial.net/sql-server-system-functions/convert-datetime-to-string/

I solved using this code.

SELECT cast(DATE_PEREMPTION as date) as DATE_PEREMPTION, ROUND(STDEV(MESURE),3) AS MESURE

FROM [MECMAS].[dbo].[T_Batch] B Join [MECMAS].[DBO].[TR_BATCH_MP] MP ON ([MP].ID_BATCH =B.ID )

Join [mecmas].[dbo].[T_DONNEES] d on (d.ID=MP.ID_DONNEES)

WHERE B.DATE_PEREMPTION >= '2022-01-01 00:00:00.00' AND NAME='AGUA' group by cast(DATE_PEREMPTION as date) ORDER BY DATE_PEREMPTION

enter image description here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM