问: 现在有一个表,里面内容是每10秒一条(1天为6*60*24=8640条),然后我现在要做的是以5分钟为一个时间点取数据(一天为12*24=288条),一次性把所有满足条件的取出来。应该怎么写? 答:sql语句如下: 每分钟为单位做数据的抽取: select node_id,avg(ad
问:
现在有一个表,里面内容是每10秒一条(1天为6*60*24=8640条),然后我现在要做的是以5分钟为一个时间点取数据(一天为12*24=288条),一次性把所有满足条件的值取出来。应该怎么写?
答:sql语句如下:
每分钟为单位做数据的抽取:
select node_id,avg(ad1_value) as ad1_value,avg(ad2_value) as ad2_value,date_format(collect_date,'%y-%m-%d %h:%i') as collect_date
from realtime_data_3
where collect_date>'2014-12-06 12:00:00'
and collect_date<'2014-12-07 12:00:00'
group by date_format(collect_date,'%y-%m-%d %h:%i');
5分为单位:select floor(recdt/500)*500 as gt,avg(nowvalue) from reg_conditigroup by gt order by gt;
10分为单位:select floor(recdt/1000)*1000 as gt,avg(nowvalue) from reg_conditigroup by gt order by gt;
date_format的语法如下:
定义和用法date_format() 函数用于以不同的格式显示日期/时间数据。
语法date_format(date,format)
date 参数是合法的日期。format 规定日期/时间的输出格式。
可以使用的格式有:
格式描述
%a 缩写星期名
%b 缩写月名
%c 月,数值
%d 带有英文前缀的月中的天
%d 月的天,数值(00-31)
%e 月的天,数值(0-31)
%f 微秒
%h 小时 (00-23)
%h 小时 (01-12)
%i 小时 (01-12)
%i 分钟,数值(00-59)
%j 年的天 (001-366)
%k 小时 (0-23)
%l 小时 (1-12)
%m 月名
%m 月,数值(00-12)
%p am 或 pm
%r 时间,12-小时(hh:mm:ss am 或 pm)
%s 秒(00-59)
%s 秒(00-59)
%t 时间, 24-小时 (hh:mm:ss)
%u 周 (00-53) 星期日是一周的第一天
%u 周 (00-53) 星期一是一周的第一天
%v 周 (01-53) 星期日是一周的第一天,与 %x 使用
%v 周 (01-53) 星期一是一周的第一天,与 %x 使用
%w 星期名
%w 周的天 (0=星期日, 6=星期六)
%x 年,其中的星期日是周的第一天,4 位,与 %v 使用
%x 年,其中的星期一是周的第一天,4 位,与 %v 使用
%y 年,4 位
%y 年,2 位
实例下面的脚本使用 date_format() 函数来显示不同的格式。我们使用 now() 来获得当前的日期/时间:
date_format(now(),'%b %d %y %h:%i %p')date_format(now(),'%m-%d-%y')date_format(now(),'%d %b %y')date_format(now(),'%d %b %y %t:%f')
结果类似:
dec 29 2008 11:45 pm12-29-200829 dec 0829 dec 2008 16:25:46.635