mysql-关于时间段的汇总查询,如何实现?

问题描述

关于时间段的汇总查询,如何实现?

sign db_username db_ip beginTime endTime
downloadFiles tom 125.92.142.224 1378063471 1378063472
downloadFiles tom 125.92.142.224 1378072128 1378072129
downloadFiles tom2 84.14.196.239 1378077892 1378077893
downloadFiles tom2 84.14.196.239 1378077893 1378077894
downloadFiles tom2 84.14.196.239 1378077894 1378077895
downloadFiles tom2 84.14.196.239 1378077895 1378077896
downloadFiles tom2 84.14.196.239 1378077896 1378077897
downloadFiles tom2 84.14.196.239 1378077897 1378077898
downloadFiles tom2 84.14.196.239 1378079181 1378079182
downloadFiles tom2 84.14.196.239 1378079181 1378079182
downloadFiles tom2 84.14.196.239 1378079182 1378079183
downloadFiles tom2 84.14.196.239 1378079182 1378079183
downloadFiles tom2 84.14.196.239 1378079184 1378079185
downloadFiles tom2 84.14.196.239 1378079184 1378079185
downloadFiles tom2 84.14.196.239 1378079184 1378079185
downloadFiles tom2 84.14.196.239 1378079185 1378079186
downloadFiles tom2 84.14.196.239 1378079185 1378079186
downloadFiles tom2 84.14.196.239 1378079185 1378079186
downloadFiles tom2 84.14.196.239 1378079188 1378079189
downloadFiles tom2 84.14.196.239 1378079188 1378079189
downloadFiles tom2 84.14.196.239 1378079189 1378079190
downloadFiles tom2 84.14.196.239 1378079190 1378079191
downloadFiles tom2 84.14.196.239 1378079190 1378079191
downloadFiles tom2 84.14.196.239 1378079191 1378079192
downloadFiles tom2 84.14.196.239 1378079192 1378079193

我数据库里有这样的数据(关于时间值,我己转为unix 时间戳,方便比较)
现在我想要查询一个用户不重复的时间段的总和(如有重复的时间段,不计入总和,只计一次)
如 tom 第一条 为 2013-09-11 18:22:34 as T1 2013-09-11 18:22:54 as T2
    第二条 为 2013-09-11 18:23:34 as T3 2013-09-11 18:24:54 as T4  
SUM=(T2-T1)+(T4-T3) boz T3>T2
如果 第二条 为 2013-09-11 18:24:00 as T3 2013-09-11 18:24:54 as T4
SUM=(T4-T1) boz T3T1
如果 第二条 为 2013-09-11 18:22:37 as T3 2013-09-11 18:22:50 as T4
sum=T2-T1 boz T3<T1 && T4<T1

想将各个用户的时间段总和求出来
结果集为
downloadFiles tom 125.92.142.224 TotalTime 15(seconds)
downloadFiles tom2 84.14.196.239 TotalTime 309(seconds)
分组为sign/username/ip
想用一句SQL或存储过程实现 
避免用通过loop来累积时间段总和,效率又太低了
大家有什么好的办法呢

如己加了标签 2013-09-11 18:22:34 as T1 2013-09-11 18:22:54 as T2
但新记录为  2013-09-11 18:18:34 as T3 2013-09-11 18:23:54 as T4
又要重新算总值

我的目的,是求一個用戶,傳輸文件的總時間,但不要重復的時間段
如3到5點,傳一百個文件,不能sum(100*2) 只能2 h
一個文件是2:00-3:00 別一個文件是 2:30-3:00  total 為 3-2=1
一個文件是2:00-3:00 別一個文件是 2:30-3:30  total 為 3.5-2=1.5

时间: 2024-09-18 09:40:08

mysql-关于时间段的汇总查询,如何实现?的相关文章

mysql按时间段分段查询

问题描述 mysql按时间段分段查询 每10分钟一组进行查询,期待结果是这种 这里我用的是 GROUP BY floor(bet_date / 1000) * 1000 ,但是有个问题是,并不是所有的时间段都有数据,也就是说会出现这种问题 没数据的地方就没有时间,我希望没有数据的地方时间继续显示,数据是0也好null也好 都可以 解决方案 mysql 各种时间段查询mysql 时间段查询mysql按时间段查询 解决方案二: 1,更改数据库字段非空设定,保证数据正确性.2,重写一条sql,加入条件

把一天分成四个时间段,分别查询这四个时间段的数据

问题描述 把一天分成四个时间段,分别查询这四个时间段的数据 0900-1200 1200-1600 1600-2100 2100-0900 这四个时间段 意思就是相当于一天的开始是9点,然后最后的时间段是晚上9点到第二天早上9点 ,查询的数据都是9点到第二天9点的数据,求大神给点思路 解决方案 LINQ么? var query = db.GroupBy(x.时间字段.Hour); 第一个时段 = query.Where(x => new int[] {9,10,11}.Contains(x.Ke

sql数据问题。某个时间段内汇总特别慢,而其他时间段汇总就很快,这是什么原因呢?

问题描述 sql数据问题.某个时间段内汇总特别慢,而其他时间段汇总就很快,这是什么原因呢? 同一个表,有时间字段,比如我现在查询2015年5月份数据特别的慢,有时候根本就查不出来,而其他月份的数据就很快. 解决方案 本文对应Sql Server 中常用的时间查询的进行一些汇总,例如查询当天的.本周的.本月的.本季度的,某个时间段内的时间. 实例实例(我的)表名:mytable 字段名:mydate (一).当天(某两个时间段) select * from mytable where DATEDI

MySQL将表a中查询的数据插入到表b中

 MySQL将表a中查询的数据插入到表b中 如果表b存在 1 <code class="sql"><code class="sql"><code class="sql">insert into b select * from a;</code></code></code> 如果表b不存在 1 <code class="sql"><code

MySQL中优化sql语句查询常用的30种方法

本篇文章是对MySQL中优化sql语句查询常用的30种方法进行了详细的分析介绍,需要的朋友参考下   1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索引而进行全表扫描. 3.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而进行全表扫描,如: select id from t where num is null 可以

money-c# linq 汇总查询问题

问题描述 c# linq 汇总查询问题 sql = select goodsid,ceiling(qs),sum(money) from md group by goodsid,ceiling(qs)我想问一个ceilling这个功能用linq如何实现呀.谢谢了. 解决方案 from x in md group x by new { goodsid, cqs = math.ceiling(qs) } into g select new { g.key.goodsid g.key.cqs,sum

mysql如何实现递归等级查询

问题描述 mysql如何实现递归等级查询 在oracle中select max(level) into max_level from zj_template_option t where t.enable = '1' and t.template_id = '5' --and level >='3' and t.ispfx = '1' start with t.pid = '0' connect by prior t.id = t.pid order by template_id; 把它改成my

mysql-关于MySQL的完全外连接查询

问题描述 关于MySQL的完全外连接查询 问题看下图 为毛左连接和右连接查询都可以,全外连接就不行了啊?而且命令我也是直接翻阅上面的,只是把right改成了full 解决方案 mysql 不支持full joinhttp://blog.csdn.net/whitebill2004/article/details/7570541 解决方案二: (A left join B ) union (A right join B) 解决方案三: select [列名称] from 表名称 cross joi

mysql-关于时间段总和的查询

问题描述 关于时间段总和的查询 sign db_username db_ip beginTime endTime downloadFiles tom 125.92.142.224 1378063471 1378063472 downloadFiles tom 125.92.142.224 1378072128 1378072129 downloadFiles tom2 84.14.196.239 1378077892 1378077893 downloadFiles tom2 84.14.196