So lets say we have data that looks likes:
drop table if exists views;
create table views(id int primary key,start time,end time);
insert into views values
(1, '15:01', '15:04'),
(2, '15:02', '15:09'),
(3, '15:12', '15:15'),
(4, '16:11', '16:23'),
(5, '16:19', '16:25'),
(6, '17:52', '17:59'),
(7, '18:18', '18:22'),
(8, '16:20', '16:22'),
(9, '18:17', '18:23');
Easily visualized like this
1 |-----|
2 |-----|
3 |--|
4 |-----|
5 |-----|
6 |---|
7 |---|
8 |---|
9 |-----|
Now I want to graph that data so it looks like this
+---------------------------+
| x |
| x x xxx xxx |
| x xx xx x xx x |
+---------------------------+
essentially breaking them up into segments of X length and summing up how many times each X length segment is touched. Any thoughts on how to create this view?
(If you must know this it so I can create Engagement Data for Video Analytics)
I dont want the output to be ASCII I want it to end up as query result in SQL. Something like:
Time Start, Time End, Num_Views
00:00, 00:05, 10
00:06, 00:10, 3
00:11, 00:15, 2
00:16, 00:20, 8
Using an auxiliary numbers table, you could do something like this:
For your particular example this script produces the following output:
A numbers table could be a temporary one, although I would recommend you to create and initialise a permanent table, as it can be useful for many purposes. Here’s one way of initialising a numbers table:
A ‘live’ version of this script can be found on SQL Fiddle.
UPDATE (an attempt at a description of the method used)
The above solution implements the following steps:
Find the earliest
starttime and the latestendtime in theviewstable.Convert both values to unix timestamps.
Divide both timestamps by 300, which essentially gives us the indexes of the corresponding 5-minute ranges (since the Epoch).
With the help of a numbers table, generate a series of 5-minute ranges covering the overall range between
startandend.Match the range list against the event times in the
viewstable (using an outer join, because we want (if we want) to account for all the ranges).Group the results by the range bounds and count the number of events in the groups. (And I’ve just noticed that the
sum(v.id is not null)in the above query could be replaced with the more concise and, in this case, more naturalcount(v.id).)