1

次のようなデータがあるとします。

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'); 

こんな感じで簡単に可視化

1     |-----| 
2        |-----| 
3                 |--| 
4                       |-----| 
5                          |-----| 
6                                  |---| 
7                                        |---|  
8                           |---| 
9                                       |-----| 

今、そのデータをグラフにしたいので、このようになります

+---------------------------+
|              x            |
|    x        x xxx     xxx |
|   x xx  xx x     xx  x    |
+---------------------------+

基本的にそれらを X 長のセグメントに分割し、各 X 長のセグメントに触れた回数を合計します。このビューを作成する方法について何か考えはありますか?

(ビデオ分析用のエンゲージメント データを作成できるように、これを知っておく必要があります)

出力を ASCII にしたくないので、最終的に SQL のクエリ結果として出力したいのです。何かのようなもの:

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
4

1 に答える 1

3

補助数値表を使用すると、次のようなことができます。

select
  r.Time_Start,
  r.Time_End,
  sum(v.id is not null) as Num_Views
from (
  select
    cast(from_unixtime((m.minstart + n.n + 0) * 300) as time) as Time_Start,
    cast(from_unixtime((m.minstart + n.n + 1) * 300) as time) as Time_End
  from (
    select
      unix_timestamp(date_format(minstart, '1970-01-01 %T')) div 300 as minstart,
      unix_timestamp(date_format(maxend  , '1970-01-01 %T')) div 300 as maxend
    from (
      select
        min(start) as minstart,
        max(end  ) as maxend
      from views
    ) s
  ) m
    cross join numbers n
  where n.n between 0 and m.maxend - minstart
) r
  left join views v on v.start < r.Time_End and v.end > r.Time_Start
group by
  r.Time_Start,
  r.Time_End
;

特定の例では、このスクリプトは次の出力を生成します。

Time_Start  Time_End  Num_Views
----------  --------  ---------
15:00:00    15:05:00  2
15:05:00    15:10:00  1
15:10:00    15:15:00  1
15:15:00    15:20:00  0
15:20:00    15:25:00  0
15:25:00    15:30:00  0
15:30:00    15:35:00  0
15:35:00    15:40:00  0
15:40:00    15:45:00  0
15:45:00    15:50:00  0
15:50:00    15:55:00  0
15:55:00    16:00:00  0
16:00:00    16:05:00  0
16:05:00    16:10:00  0
16:10:00    16:15:00  1
16:15:00    16:20:00  2
16:20:00    16:25:00  3
16:25:00    16:30:00  0
16:30:00    16:35:00  0
16:35:00    16:40:00  0
16:40:00    16:45:00  0
16:45:00    16:50:00  0
16:50:00    16:55:00  0
16:55:00    17:00:00  0
17:00:00    17:05:00  0
17:05:00    17:10:00  0
17:10:00    17:15:00  0
17:15:00    17:20:00  0
17:20:00    17:25:00  0
17:25:00    17:30:00  0
17:30:00    17:35:00  0
17:35:00    17:40:00  0
17:40:00    17:45:00  0
17:45:00    17:50:00  0
17:50:00    17:55:00  1
17:55:00    18:00:00  1
18:00:00    18:05:00  0
18:05:00    18:10:00  0
18:10:00    18:15:00  0
18:15:00    18:20:00  2
18:20:00    18:25:00  2

数値テーブルは一時的なものである可能性がありますが、多くの目的に役立つ可能性があるため、永続的なテーブルを作成して初期化することをお勧めします. 数値テーブルを初期化する 1 つの方法を次に示します。

create table numbers (n int);
insert into numbers (n) select 0;
insert into numbers (n) select cnt + n from numbers, (select count(*) as cnt from numbers) s;
insert into numbers (n) select cnt + n from numbers, (select count(*) as cnt from numbers) s;
insert into numbers (n) select cnt + n from numbers, (select count(*) as cnt from numbers) s;
insert into numbers (n) select cnt + n from numbers, (select count(*) as cnt from numbers) s;
insert into numbers (n) select cnt + n from numbers, (select count(*) as cnt from numbers) s;
insert into numbers (n) select cnt + n from numbers, (select count(*) as cnt from numbers) s;
insert into numbers (n) select cnt + n from numbers, (select count(*) as cnt from numbers) s;
insert into numbers (n) select cnt + n from numbers, (select count(*) as cnt from numbers) s;
/* repeat as necessary; every repeated line doubles the number of rows */

このスクリプトの「ライブ」バージョンはSQL Fiddle にあります。

UPDATE (使用された方法の説明の試み)

上記のソリューションは、次の手順を実装します。

  1. start表で最も早い時刻と最も遅いend時刻を見つけviewsます。

  2. 両方の値をUNIX タイムスタンプに変換します。

  3. 両方のタイムスタンプを 300 で割ると、基本的に対応する 5 分間の範囲のインデックスが得られます (エポック以降)。

  4. 数値表を使用して、 ~ の間の範囲全体をカバーする一連の 5 分間の範囲を生成しstartますend

  5. 範囲リストをテーブル内のイベント時間と照合します (すべての範囲を考慮したい場合viewsは、外部結合を使用します)。

  6. 範囲の境界で結果をグループ化し、グループ内のイベントの数を数えます。sum(v.id is not null)(そして、上記のクエリの the は、より簡潔で、この場合はより自然な に置き換えることができることに気付きましたcount(v.id)。)

于 2012-05-08T07:27:59.750 に答える