1.日期中的重叠问题
建表sessions:

CREATE TABLE `sessions` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`app` varchar(10) NOT NULL,
`usr` varchar(10) NOT NULL,
`starttime` time NOT NULL,
`endtime` time NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB

插入记录:

insert into sessions(app,usr,starttime,endtime) values('app1','user1','08:30','08:45');
insert into sessions(app,usr,starttime,endtime) values('app1','user2','09:00','09:30');
insert into sessions(app,usr,starttime,endtime) values('app1','user1','09:15','10:30');
insert into sessions(app,usr,starttime,endtime) values('app1','user2','09:15','09:30');
insert into sessions(app,usr,starttime,endtime) values('app1','user1','10:30','14:30');
insert into sessions(app,usr,starttime,endtime) values('app1','user2','10:45','11:30');
insert into sessions(app,usr,starttime,endtime) values('app1','user1','11:00','12:30');
insert into sessions(app,usr,starttime,endtime) values('app2','user1','08:30','08:45');
insert into sessions(app,usr,starttime,endtime) values('app2','user1','08:30','08:45');
insert into sessions(app,usr,starttime,endtime) values('app2','user2','09:00','09:30');
insert into sessions(app,usr,starttime,endtime) values('app2','user1','11:45','12:00');
insert into sessions(app,usr,starttime,endtime) values('app2','user2','12:30','14:00');
insert into sessions(app,usr,starttime,endtime) values('app2','user1','12:45','13:30');
insert into sessions(app,usr,starttime,endtime) values('app2','user2','13:00','14:00');
insert into sessions(app,usr,starttime,endtime) values('app2','user1','14:00','16:30');
insert into sessions(app,usr,starttime,endtime) values('app2','user2','15:30','17:00');

创建索引,加快查询速度:

mysql> create unique index idx_app_usr_s_e_key on sessions(app,usr,starttime,endtime,id);
Query OK, 0 rows affected (0.23 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> create index idx_app_s_e on sessions(app,starttime,endtime);
Query OK, 0 rows affected (0.22 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index in sessions;
+----------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+----------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| sessions | 0 | PRIMARY | 1 | id | A | 2 | NULL | NULL | | BTREE | | |
| sessions | 0 | idx_app_usr_s_e_key | 1 | app | A | 16 | NULL | NULL | | BTREE | | |
| sessions | 0 | idx_app_usr_s_e_key | 2 | usr | A | 16 | NULL | NULL | | BTREE | | |
| sessions | 0 | idx_app_usr_s_e_key | 3 | starttime | A | 16 | NULL | NULL | | BTREE | | |
| sessions | 0 | idx_app_usr_s_e_key | 4 | endtime | A | 16 | NULL | NULL | | BTREE | | |
| sessions | 0 | idx_app_usr_s_e_key | 5 | id | A | 16 | NULL | NULL | | BTREE | | |
| sessions | 1 | idx_app_s_e | 1 | app | A | 16 | NULL | NULL | | BTREE | | |
| sessions | 1 | idx_app_s_e | 2 | starttime | A | 16 | NULL | NULL | | BTREE | | |
| sessions | 1 | idx_app_s_e | 3 | endtime | A | 16 | NULL | NULL | | BTREE | | |
+----------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
9 rows in set (0.00 sec)

重叠的分类:标示重叠,分组重叠,最大重叠

标示重叠:为每个会话标示出相同应用程序用户重叠及最大重叠会话数

mysql> select a.app,a.usr,a.starttime,a.endtime,b.starttime,b.endtime from sessions a,sessions b where a.app=b.app and a.usr=b.usr and (b.endtime>=a.starttime and b.starttime<=a.endtime);

 

+------+-------+-----------+----------+-----------+----------+
| app | usr | starttime | endtime | starttime | endtime |
+------+-------+-----------+----------+-----------+----------+
| app1 | user1 | 08:30:00 | 08:45:00 | 08:30:00 | 08:45:00 |
| app1 | user1 | 09:15:00 | 10:30:00 | 09:15:00 | 10:30:00 |
| app1 | user1 | 09:15:00 | 10:30:00 | 10:30:00 | 14:30:00 |
| app1 | user1 | 10:30:00 | 14:30:00 | 09:15:00 | 10:30:00 |
| app1 | user1 | 10:30:00 | 14:30:00 | 10:30:00 | 14:30:00 |
| app1 | user1 | 10:30:00 | 14:30:00 | 11:00:00 | 12:30:00 |
| app1 | user1 | 11:00:00 | 12:30:00 | 10:30:00 | 14:30:00 |
| app1 | user1 | 11:00:00 | 12:30:00 | 11:00:00 | 12:30:00 |
| app1 | user2 | 09:00:00 | 09:30:00 | 09:00:00 | 09:30:00 |
| app1 | user2 | 09:00:00 | 09:30:00 | 09:15:00 | 09:30:00 |
| app1 | user2 | 09:15:00 | 09:30:00 | 09:00:00 | 09:30:00 |
| app1 | user2 | 09:15:00 | 09:30:00 | 09:15:00 | 09:30:00 |
| app1 | user2 | 10:45:00 | 11:30:00 | 10:45:00 | 11:30:00 |
| app2 | user1 | 08:30:00 | 08:45:00 | 08:30:00 | 08:45:00 |
| app2 | user1 | 08:30:00 | 08:45:00 | 08:30:00 | 08:45:00 |
| app2 | user1 | 08:30:00 | 08:45:00 | 08:30:00 | 08:45:00 |
| app2 | user1 | 08:30:00 | 08:45:00 | 08:30:00 | 08:45:00 |
| app2 | user1 | 11:45:00 | 12:00:00 | 11:45:00 | 12:00:00 |
| app2 | user1 | 12:45:00 | 13:30:00 | 12:45:00 | 13:30:00 |
| app2 | user1 | 14:00:00 | 16:30:00 | 14:00:00 | 16:30:00 |
| app2 | user2 | 09:00:00 | 09:30:00 | 09:00:00 | 09:30:00 |
| app2 | user2 | 12:30:00 | 14:00:00 | 12:30:00 | 14:00:00 |
| app2 | user2 | 12:30:00 | 14:00:00 | 13:00:00 | 14:00:00 |
| app2 | user2 | 13:00:00 | 14:00:00 | 12:30:00 | 14:00:00 |
| app2 | user2 | 13:00:00 | 14:00:00 | 13:00:00 | 14:00:00 |
| app2 | user2 | 15:30:00 | 17:00:00 | 15:30:00 | 17:00:00 |
+------+-------+-----------+----------+-----------+----------+
26 rows in set (0.00 sec)

 

分组重叠:服务商可能允许多个session的连接,并把其计费统计为1次,这就是所谓的分组重叠,对于例子中应该把app1,user1在08:30--10:30合并算为一次会话.
如下:

mysql> select distinct app,usr,starttime as s from sessions as a where not exists(select * from sessions as b where a.app=b.app and a.usr=b.usr and a.starttime>b.starttime and a.starttime<=b.endtime);
+------+-------+----------+
| app | usr | s |
+------+-------+----------+
| app1 | user1 | 08:30:00 |
| app1 | user1 | 09:15:00 |
| app1 | user2 | 09:00:00 |
| app1 | user2 | 10:45:00 |
| app2 | user1 | 08:30:00 |
| app2 | user1 | 11:45:00 |
| app2 | user1 | 12:45:00 |
| app2 | user1 | 14:00:00 |
| app2 | user2 | 09:00:00 |
| app2 | user2 | 12:30:00 |
| app2 | user2 | 15:30:00 |
+------+-------+----------+
11 rows in set (0.01 sec)
mysql> select distinct app,usr,starttime as e from sessions as a where not exists(select * from sessions as b where a.app=b.app and a.usr=b.usr and a.endtime>=b.starttime and a.endtime<b.endtime);
+------+-------+----------+
| app | usr | e |
+------+-------+----------+
| app1 | user1 | 08:30:00 |
| app1 | user1 | 10:30:00 |
| app1 | user2 | 09:00:00 |
| app1 | user2 | 09:15:00 |
| app1 | user2 | 10:45:00 |
| app2 | user1 | 08:30:00 |
| app2 | user1 | 11:45:00 |
| app2 | user1 | 12:45:00 |
| app2 | user1 | 14:00:00 |
| app2 | user2 | 09:00:00 |
| app2 | user2 | 12:30:00 |
| app2 | user2 | 13:00:00 |
| app2 | user2 | 15:30:00 |
+------+-------+----------+
13 rows in set (0.00 sec)

创建视图:v_s和v_e

mysql> create view v_s as select distinct app,usr,starttime as s from sessions as a where not exists(select * from sessions as b where a.app=b.app and a.usr=b.usr and a.starttime>b.starttime and a.starttime<=b.endtime);
mysql> create view v_e as select distinct app,usr,starttime as e from sessions as a where not exists(select * from sessions as b where a.app=b.app and a.usr=b.usr and a.endtime>=b.starttime and a.endtime<b.endtime);

未完待续......

mysql--SQL编程(关于mysql中的日期,关于重叠) 学习笔记2.2的更多相关文章

  1. C#同步,异步的理解,包括5.0中await和async(学习笔记)

    之前在工作中一直用的是同步线程,就是先进入画面的load事件,然后在里面进行数据库调用的处理.后面又遇到了公司软件中一些比较古老的代码,一开始在那块古老代码中增加机能的时候,我想用到数据库的数据给画面 ...

  2. Oracle使用触发器和mysql中使用触发器的比较——学习笔记

    一.触发器 1.触发器在数据库里以独立的对象存储, 2.触发器不需要调用,它由一个事件来触发运行 3.触发器不能接收参数 --触发器的应用 举个例子:校内网.开心网.facebook,当你发一个日志, ...

  3. 网络编程1--毕向东java基础教程视频学习笔记

    目录: 01 网络编程概述1 02 网络编程概述2 03网络编程 网络模型 04网络编程 IP地址 05网络编程 TCP和UDP 06网络编程 Socket 07网络编程 UDP发送端 01 网络编程 ...

  4. 《C#高级编程(第六版)》泛型学习笔记(一):泛型优点和特性 (转载)

    原文出处:http://www.cnblogs.com/xun126/archive/2011/01/13/1933838.html 泛型是CLR 2.0的一个新特性,在CLR 1.0中,要创建一个灵 ...

  5. Python 中的map和reduce学习笔记

    map和reduce都是Python中的内置函数 map函数接受两个参数,第一个参数是函数,第二个参数是列表,将函数依次作用于列表中的元素,并返回一个元素 reduce同样以函数和列表作为参数,区别在 ...

  6. 网络编程4--毕向东java基础教程视频学习笔记

    Day24 06 自定义浏览器-Tomcat服务端07 自定义图形界面浏览器-Tomcat服务端08 URL-URLConnection09 小知识点10 域名解析 06 自定义浏览器-Tomcat服 ...

  7. 网络编程3--毕向东java基础教程视频学习笔记

    Day24 01 TCP上传图片02 客户端并发上传图片03 客户端并发登录04 浏览器客户端-自定义服务端05 浏览器客户端-Tomcat服务端 01 TCP上传图片 import java.net ...

  8. 网络编程2--毕向东java基础教程视频学习笔记

    Day 23 08 Udp接收端09 Udp键盘录入数据方式10 Udp聊天11 TCP传输12 TCP传输213 TCP练习14 TCP复制文件 08 Udp接收端 需求:定义一个应用程序,用于接收 ...

  9. contiki-main.c 中的process系列函数学习笔记 <contiki学习笔记之六>

    说明:本文依然依赖于 contiki/platform/native/contiki-main.c 文件. ---------------------------------------------- ...

随机推荐

  1. Java去掉Html标签的方法

    content = content.replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll("< ...

  2. 【Scala】Scala-None-null引发的血案

    Scala-None-null引发的血案 Overview - Spark 2.2.0 Documentation Spark Streaming - Spark 2.2.0 Documentatio ...

  3. String escape/unescape into XML

    Is there any C# function which could be used to escape and un-escape a string, which could be used t ...

  4. moodle安装体验

    EasyPHP-5.3.8.0+moodle 2.6.2 安装过程要耐心等待. 成功安装后.网页又訪问不了. 不知道为什么? 2015/04/12 EasyPHP-DevServer-14.1VC11 ...

  5. SuperMap iDesktop之导入数据

    SuperMap作为一个平台软件有自己的数据格式,现要将ESRI的SHP数据导入到SuperMap的udb数据库中,可以完成导入,但也不得不说几点问题. 下面是ArcGIS中批量导入SHP的操作界面. ...

  6. TCP连接的建立和断开

    1.TCP连接的建立            设主机B运行一个服务器进程,它先发出一个被动打开命令,告诉它的TCP要准备接收客户进程的连续请求,然后服务进程就处于听的状态.不断检测是否有客户进程发起连续 ...

  7. 在Windows中监视IO性能

    附:在Windows中监视IO性能 本来准备写一篇windows中监视IO性能的,后来发现好像可写的内容不多,windows在细节这方面做的不是那么的好,不过那些基本信息还是有的. 在Windows中 ...

  8. 保密员(baomi)

    #include<iostream> #include<string> #include<stdio.h> #include<algorithm> #i ...

  9. jstat 使用日志

    如何判断JVM是否存在内存问题呢?如何判断JVM垃圾回收是否正常?一般的top指令基本上满足不了这样的需求,因为它主要监控的是总体的系统资源,很难定位到java应用程序. Jstat是JDK自带的一个 ...

  10. MySQL登陆小问题

    root用户创建用户 CREATE USER gechong IDENTIFIED BY 'gechong'; 登录数据库时提示错误: C:\Users\gechong>mysql -u gec ...