1. 按照时间段统计的sql语句
我只能大概说一个想法,
创建一个中间表,存放8点-14点 14点-20点 20点-2点 2点-8点 这样的一个标题及具体的时间段,
然后用这个中间表与biao1根据时间tm进行关联,就可以把数据与时间段联系上。
然后按这个时间段进行了group by ,之后再用 pivot就可以出来了。
2. SQLServer的分时间段统计的SQL语句。
没看懂a和b二个表的用途,假设对表a统计。
直接统计比较困难,不过你可以先把日期转换成到最早日期的分钟数,把分钟数除以10求整,得到的新值就是每10分钟一组的了。不过新值需要保存到临时表,再对临时表分组统计。
--按上述分析将a表中的f_time转换为以最早日期为起点的每10分钟一个值的新列,如间隔8分钟,新列值为0,18分钟,新值为1.此列命名为ten,把它们复制到临时表#tj中。
select floor(datediff(mi,(select min(f_time) from a),optime)/10) ten,f_time,f_val from a into #tj where not a is null order by f_time
--对#tj按ten列分组统计,同时显示每组的起始日期好截止日期。
select ten,min(f_time) bg,max(f_time) end,sum(f_val) hj_val from #tj group by ten order by ten
--把临时表删掉
drop table #tj
3. SQL 时间间隔 可设如1分钟5分钟等,查询相关数据。
假如表名:tbl;时间字段名:dt
select * from tbl
where dt between cast('2012-10-10 12:01:00' as datetime) and cast('2012-10-11 11:05:00' as datetime) -- 在 2012.10.10.12:1:00 和 2012.10.11.14:5:00 之间
and datediff(minute,cast('2012-10-10 12:01:00' as datetime),dt) %7 = 0 --间隔7分钟
4. SQL 分时段统计数据条数
做个时间基表 跟数据做个左连接分组统计即可
5. SQL,按5分钟统计发送数量,SQL语句怎么写呀
最简单方法,把datetime转成float型(单位整数部分为天),然后乘24*60/5,就是整数部分是5分钟了,然后取整就行了
用这种方法做,随便你算几分钟的分组都能算
如:
select getdate(),cast(floor(cast(getdate() as float)*24*60/5)*5/60/24 as smalldatetime)
----------------------- -----------------------
2010-09-16 19:19:34.547 2010-09-16 19:15:00
(1 行受影响)
select getdate(),substring(convert(varchar,cast(floor(cast(getdate() as float)*24*60/5)*5/60/24 as smalldatetime),120),12,5)
----------------------- ----------
2010-09-16 19:23:47.340 19:20
(1 行受影响)
那么你的这个问题
select
substring(convert(varchar,cast(floor(cast(sendtime as float)*24*60/5)*5/60/24 as smalldatetime),120),12,5)
,sum(concount)
from 表名
group by substring(convert(varchar,cast(floor(cast(sendtime as float)*24*60/5)*5/60/24 as smalldatetime),120),12,5)
6. mysql按10分钟,分组统计数据,如何统计
2
3
4
5
6
7
8
9
10
11
12
13
14
-- time_str '2016-11-20 04:31:11'
-- date_str 20161120
select concat(left(date_format(time_str, '%y-%m-%d %h:%i'),15),'0') as time_flag, count(*) as count from `security`.`cmd_info` where `date_str`=20161120 group by time_flag order by time_flag; -- 127 rows
select round(unix_timestamp(time_str)/(10 * 60)) as timekey, count(*) from `security`.`cmd_info` where `date_str`=20161120 group by timekey order by timekey; -- 126 rows
-- 以上2个SQL语句的思路类似——使用“group by”进行区分,但是方法有所不同,前者只能针对10分钟(或1小时)级别,后者可以动态调整间隔大小,两者效率差不多,可以根据实际情况选用
select concat(date(time_str),' ',hour(time_str),':',round(minute(time_str)/10,0)*10), count(*) from `security`.`cmd_info` where `date_str`=20161120 group by date(time_str), hour(time_str), round(minute(time_str)/10,0)*10; -- 145 rows
select concat(date(time_str),' ',hour(time_str),':',floor(minute(time_str)/10)*10), count(*) from `security`.`cmd_info` where `date_str`=20161120 group by date(time_str), hour(time_str), floor(minute(time_str)/10)*10; -- 127 rows (和 date_format 那个等价)
7. sql 根据时间段分组统计数据
把小时、分钟分别定义变量,循环变化。
8. SQL中,有一个分钟数据库,用什么指令能够生成小时、天、月统计数据库
用 分钟/60 分组 应该就是小时吧
我觉得可以这样类推
9. Oracle 取过去一个小时每分钟的数据应该如何写sql
写个例子吧
创建表、插入数据:
createtablea
(timedate);
insertintoavalues(to_date(20151208150101,'yyyymmddhh24miss'));
执行:
withtas
(<=60)
selectt.rn-1rn,sum(casewhent.rn-1=to_char(a.time,'mi')then1else0end)fromtleftjoinaont.rn=to_char(a.time,'mi')andto_char(a.time,'yyyy-mm-ddhh24:mi:ss')between
'2015-12-0815:00:00'and'2015-12-0815:59:59'
groupbyt.rn
orderbyt.rn
结果:
其中rn代表分钟数