All pastes #15029 Raw Edit

Miscellany

public sql v1 · immutable
#15029 ·published 2005-06-19 18:06 UTC
rendered paste body
# here is the test table.create table cdr (  call_id         bigint auto_increment primary key,  call_cid        varchar(32),  call_csid       varchar(32),  call_start      timestamp,  call_end        timestamp,  index call_start_i (call_start),  index call_end_i (call_end));# Paste this by hand about 80 times to generate sample data.set @ts:=(now() - interval cast(rand()*1000 as SIGNED) second); insert into cdr VALUES (null, concat('360555', lpad((cast(rand()*1000 as SIGNED)),4,'0')), concat('206555', lpad((cast(rand()*1000 as SIGNED)),4,'0')), @ts, (@ts + interval cast(rand()*1000 as SIGNED) second) );# then query it :)select a.call_start, (select count(call_id) from cdr as b where ( b.call_start <= a.call_start and b.call_end >= a.call_start) ) as 'Simultanious' from cdr as a group by call_start order by Simultanious;# my samplemysql> select * from cdr;+---------+------------+------------+---------------------+---------------------+| call_id | call_cid   | call_csid  | call_start          | call_end            |+---------+------------+------------+---------------------+---------------------+|       1 | 3605550653 | 2065550072 | 2005-06-19 10:44:59 | 2005-06-19 10:51:42 ||       2 | 3605550784 | 2065550524 | 2005-06-19 10:43:59 | 2005-06-19 10:48:28 ||       3 | 3605550064 | 2065550999 | 2005-06-19 10:44:24 | 2005-06-19 10:57:47 ||       4 | 3605550691 | 2065550395 | 2005-06-19 10:56:58 | 2005-06-19 11:12:01 ||       5 | 3605550937 | 2065550700 | 2005-06-19 10:51:50 | 2005-06-19 11:03:18 ||       6 | 3605550642 | 2065550185 | 2005-06-19 10:51:38 | 2005-06-19 10:51:41 ||       7 | 3605550279 | 2065550024 | 2005-06-19 10:49:42 | 2005-06-19 10:54:26 ||       8 | 3605550885 | 2065550384 | 2005-06-19 10:51:32 | 2005-06-19 10:55:58 ||       9 | 3605550099 | 2065550957 | 2005-06-19 10:54:20 | 2005-06-19 11:02:29 ||      10 | 3605550413 | 2065550336 | 2005-06-19 10:47:44 | 2005-06-19 10:55:04 ||      11 | 3605550655 | 2065550691 | 2005-06-19 10:54:05 | 2005-06-19 11:02:15 ||      12 | 3605550418 | 2065550956 | 2005-06-19 10:51:15 | 2005-06-19 11:00:03 ||      13 | 3605550275 | 2065550062 | 2005-06-19 10:44:40 | 2005-06-19 10:52:46 ||      14 | 3605550765 | 2065550094 | 2005-06-19 10:53:27 | 2005-06-19 10:56:19 ||      15 | 3605550389 | 2065550203 | 2005-06-19 10:47:51 | 2005-06-19 11:02:00 ||      16 | 3605550640 | 2065550289 | 2005-06-19 10:46:54 | 2005-06-19 10:55:38 ||      17 | 3605550193 | 2065550709 | 2005-06-19 10:45:00 | 2005-06-19 11:01:04 ||      18 | 3605550582 | 2065550824 | 2005-06-19 10:45:58 | 2005-06-19 10:52:16 ||      19 | 3605550946 | 2065550485 | 2005-06-19 10:50:37 | 2005-06-19 11:00:23 ||      20 | 3605550631 | 2065550720 | 2005-06-19 10:49:35 | 2005-06-19 11:01:22 ||      21 | 3605550762 | 2065550680 | 2005-06-19 10:51:17 | 2005-06-19 10:53:13 ||      22 | 3605550352 | 2065550139 | 2005-06-19 10:48:33 | 2005-06-19 10:59:15 ||      23 | 3605550030 | 2065550778 | 2005-06-19 10:44:23 | 2005-06-19 10:57:46 ||      24 | 3605550982 | 2065550878 | 2005-06-19 10:46:16 | 2005-06-19 10:53:42 ||      25 | 3605550630 | 2065550371 | 2005-06-19 10:47:41 | 2005-06-19 11:03:45 ||      26 | 3605550643 | 2065550095 | 2005-06-19 10:45:47 | 2005-06-19 10:54:55 ||      27 | 3605550621 | 2065550750 | 2005-06-19 10:50:01 | 2005-06-19 11:04:45 ||      28 | 3605550208 | 2065550523 | 2005-06-19 10:54:42 | 2005-06-19 11:11:14 ||      29 | 3605550984 | 2065550743 | 2005-06-19 10:51:03 | 2005-06-19 11:03:49 ||      30 | 3605550699 | 2065550698 | 2005-06-19 10:47:36 | 2005-06-19 10:54:09 ||      31 | 3605550176 | 2065550268 | 2005-06-19 10:43:04 | 2005-06-19 10:56:36 ||      32 | 3605550845 | 2065550457 | 2005-06-19 10:53:19 | 2005-06-19 11:05:50 ||      33 | 3605550678 | 2065550233 | 2005-06-19 10:51:09 | 2005-06-19 10:53:20 ||      34 | 3605550388 | 2065550072 | 2005-06-19 10:41:39 | 2005-06-19 10:44:56 ||      35 | 3605550254 | 2065550962 | 2005-06-19 10:44:46 | 2005-06-19 10:45:35 ||      36 | 3605550661 | 2065550222 | 2005-06-19 10:51:34 | 2005-06-19 10:53:39 ||      37 | 3605550423 | 2065550237 | 2005-06-19 10:41:37 | 2005-06-19 10:56:56 ||      38 | 3605550650 | 2065550609 | 2005-06-19 10:42:55 | 2005-06-19 10:44:30 ||      39 | 3605550965 | 2065550874 | 2005-06-19 10:46:46 | 2005-06-19 10:54:43 ||      40 | 3605550387 | 2065550644 | 2005-06-19 10:44:52 | 2005-06-19 10:45:52 ||      41 | 3605550657 | 2065550185 | 2005-06-19 10:51:29 | 2005-06-19 11:07:23 ||      42 | 3605550218 | 2065550443 | 2005-06-19 10:54:01 | 2005-06-19 11:03:22 ||      43 | 3605550705 | 2065550095 | 2005-06-19 10:49:39 | 2005-06-19 10:55:36 ||      44 | 3605550445 | 2065550716 | 2005-06-19 10:49:14 | 2005-06-19 10:53:20 ||      45 | 3605550679 | 2065550146 | 2005-06-19 10:56:13 | 2005-06-19 11:07:45 ||      46 | 3605550046 | 2065550155 | 2005-06-19 10:57:13 | 2005-06-19 11:07:52 ||      47 | 3605550728 | 2065550455 | 2005-06-19 10:45:28 | 2005-06-19 10:46:59 ||      48 | 3605550178 | 2065550621 | 2005-06-19 10:56:08 | 2005-06-19 11:05:40 ||      49 | 3605550276 | 2065550383 | 2005-06-19 10:41:00 | 2005-06-19 10:42:29 ||      50 | 3605550223 | 2065550221 | 2005-06-19 10:52:40 | 2005-06-19 10:59:55 ||      51 | 3605550262 | 2065550773 | 2005-06-19 10:49:05 | 2005-06-19 10:50:22 ||      52 | 3605550110 | 2065550347 | 2005-06-19 10:56:30 | 2005-06-19 11:03:15 ||      53 | 3605550710 | 2065550594 | 2005-06-19 10:41:13 | 2005-06-19 10:55:15 ||      54 | 3605550614 | 2065550789 | 2005-06-19 10:50:31 | 2005-06-19 10:52:11 |+---------+------------+------------+---------------------+---------------------+54 rows in set (0.00 sec)# output+---------------------+--------------+| call_start          | Simultanious |+---------------------+--------------+| 2005-06-19 10:41:00 |            1 || 2005-06-19 10:41:13 |            2 || 2005-06-19 10:41:37 |            3 || 2005-06-19 10:41:39 |            4 || 2005-06-19 10:42:55 |            4 || 2005-06-19 10:43:04 |            5 || 2005-06-19 10:43:59 |            6 || 2005-06-19 10:44:23 |            7 || 2005-06-19 10:44:24 |            8 || 2005-06-19 10:44:40 |            8 || 2005-06-19 10:44:46 |            9 || 2005-06-19 10:44:52 |           10 || 2005-06-19 10:44:59 |           10 || 2005-06-19 10:45:00 |           11 || 2005-06-19 10:45:58 |           12 || 2005-06-19 10:45:28 |           12 || 2005-06-19 10:45:47 |           12 || 2005-06-19 10:46:16 |           13 || 2005-06-19 10:46:46 |           14 || 2005-06-19 10:46:54 |           15 || 2005-06-19 10:47:36 |           15 || 2005-06-19 10:47:41 |           16 || 2005-06-19 10:47:44 |           17 || 2005-06-19 10:47:51 |           18 || 2005-06-19 10:48:33 |           18 || 2005-06-19 10:49:05 |           19 || 2005-06-19 10:49:14 |           20 || 2005-06-19 10:49:35 |           21 || 2005-06-19 10:49:39 |           22 || 2005-06-19 10:56:08 |           23 || 2005-06-19 10:56:58 |           23 || 2005-06-19 10:49:42 |           23 || 2005-06-19 10:56:13 |           24 || 2005-06-19 10:56:30 |           24 || 2005-06-19 10:57:13 |           24 || 2005-06-19 10:50:01 |           24 || 2005-06-19 10:50:31 |           24 || 2005-06-19 10:50:37 |           25 || 2005-06-19 10:51:03 |           26 || 2005-06-19 10:51:09 |           27 || 2005-06-19 10:51:15 |           28 || 2005-06-19 10:54:01 |           28 || 2005-06-19 10:54:42 |           29 || 2005-06-19 10:51:17 |           29 || 2005-06-19 10:53:27 |           29 || 2005-06-19 10:54:05 |           29 || 2005-06-19 10:54:20 |           29 || 2005-06-19 10:51:29 |           30 || 2005-06-19 10:53:19 |           30 || 2005-06-19 10:51:32 |           31 || 2005-06-19 10:52:40 |           31 || 2005-06-19 10:51:34 |           32 || 2005-06-19 10:51:50 |           32 || 2005-06-19 10:51:38 |           33 |+---------------------+--------------+54 rows in set (0.00 sec)