在间隔结束时平均

时间:2016-06-03 01:25:23

标签: mysql

我创建了一个SQL视图,每10分钟平均一次表格数据:

SELECT 
    DATE_FORMAT(FROM_UNIXTIME((FLOOR((UNIX_TIMESTAMP(`test_data`.`DTS`) / (10 * 60))) * (10 * 60))),
            '%m/%d/%y %H:%i') AS `AVG DTS`,
    AVG(`test_data`.`A`) AS `AVG A`,
    AVG(`test_data`.`B`) AS `AVG B`,
    AVG(`test_data`.`C`) AS `AVG C`
FROM
    `test_data`
WHERE
    (`test_data`.`DTS` BETWEEN '2016-04-22 00:00' AND '2016-05-03 00:00')
GROUP BY `AVG DTS`

该表格如下:(注意:时间戳不是故意排序的,因为这可能是数据在现实世界中放入表格的方式)

 DTS,A,B,C
"2016-04-22 12:01:00",2,5,8
"2016-04-22 12:02:00",3,6,9
"2016-04-22 12:03:00",4,7,1
"2016-04-22 12:04:00",5,8,2
"2016-04-22 12:05:00",6,9,3
"2016-04-22 12:06:00",7,1,4
"2016-04-22 12:07:00",8,2,5
"2016-04-22 12:08:00",9,3,6
"2016-04-22 12:09:00",1,4,7
"2016-04-22 12:10:00",2,5,8
"2016-04-22 12:11:00",3,6,9
"2016-04-22 12:12:00",4,7,1
"2016-04-22 12:13:00",5,8,2
"2016-04-22 12:14:00",6,9,3
"2016-04-22 12:15:00",7,1,4
"2016-04-22 12:16:00",8,2,5
"2016-04-22 12:17:00",9,3,6
"2016-04-22 12:18:00",1,4,7
"2016-04-22 12:19:00",2,5,8
"2016-04-22 12:20:00",3,6,9
"2016-04-22 12:00:00",1,4,7
"2016-04-22 12:21:00",4,7,1
"2016-04-22 12:22:00",5,8,2
"2016-04-22 12:23:00",6,9,3
"2016-04-22 12:24:00",7,1,4
"2016-04-22 12:25:00",8,2,5
"2016-04-22 12:26:00",9,3,6

输出如下:

"AVG DTS","AVG A","AVG B","AVG C"
"04/22/16 12:00",4.6000,4.9000,5.2000
"04/22/16 12:10",4.7000,5.0000,5.3000
"04/22/16 12:20",6.0000,5.1429,4.2857

12:00的AVG DTS是从12:00到12:09的相应列的表条目的平均值,AVG DTS表示12:10是表条目的平均值从12:10到12:19的相应列等等。

我希望第一个输出为AVG DTS 12:10,这将是从12:01到12:10的相应列的表条目的平均值。下一个AVG DTS输出将是12:20,并且是从12:11到12:20的相应列的表条目的平均值。等

此外,如果有更好的方法来编码,请告诉我。

日Thnx

0 个答案:

没有答案