2017-12-28 13 views
-1

私はクエリロジックがついていません。テーブル構造のイメージを見つけてください。私は1つのテーブルが2つのテーブルを持っていて、どのユーザーがどのテレビのチャンネルを視聴したのか、そこにはすべてのチャンネルのリストがあるテーブルがあります。 。チャネルとプログラムによって次のデータを表示するSQLクエリを作成するにはどうすればよいですか?

私はチャネルテーブルを使用してユーザテーブルを爆発させる必要があります。チャンネル間では、指定された時間にチャネルが再生されます。

enter image description here

+2

後に停止しbb
を停止した後に停止し中の開始あなたが作った努力の –

+2

期待される結果は何ですか? _explodeはあなたのユーザーテーブルを意味しますか? – Sami

+0

DDLとサンプル・データをSQLスクリプトとして提供した方が良いでしょう。 – Eralper

答えて

0

あなたはここでは、SELECT文

select 
*, 
case 
    when (u.starttime <= c.start and u.endtime >= c.[end]) 
    then datediff(mi, c.start,c.[end]) 
    when (c.start <= u.starttime and c.[end] >= u.endtime) 
    then datediff(mi, u.starttime,u.endtime) 
    when (u.endtime between c.start and c.[end]) 
    then datediff(mi, c.start, u.endtime) 
    when (c.[end] between u.starttime and u.endtime) 
    then datediff(mi, u.starttime,c.[end]) 
    end as period 
from [user] u 
inner join channel c 
    on u.channel = c.channel and (
     u.starttime between c.start and c.[end] or 
     u.endtime between c.start and c.[end] or 
     (u.starttime <= c.start and u.endtime >= c.[end]) or 
     (c.start <= u.starttime and c.[end] >= u.endtime) 
     ) 

を、以下の結果を確認してください可能性があり、それぞれが前に開始したときに

create table [user] (
    name varchar(10), 
    aga int, 
    channel varchar(10), 
    starttime time, 
    endtime time 
) 
    create table channel (
    channel varchar(10), 
    title varchar(10), 
    [start] time, 
    [end] time 
) 

insert into [user] select 'a',12,'abc','0:10','0:15' 
insert into [user] select 'a',12,'abc','0:16','1:00' 
insert into [user] select 'a',12,'xyz','1:10','1:30' 
insert into [user] select 'a',12,'xyz','1:40','1:50' 
insert into [user] select 'a',12,'xyz','2:20','2:40' 
insert into [user] select 'a',12,'xyz','2:40','3:00' 
insert into [user] select 'a',12,'abc','5:30','6:40' 
insert into [channel] select 'abc','a','0:00','0:12' 
insert into [channel] select 'abc','b','0:12','0:15' 
insert into [channel] select 'abc','c','0:15','0:30' 
insert into [channel] select 'abc','1','0:30','0:55' 
insert into [channel] select 'abc','2','0:45','1:15' 
insert into [channel] select 'abc','3','5:00','5:35' 
insert into [channel] select 'abc','4','5:35','6:35' 
insert into [channel] select 'abc','5','6:35','7:35' 
insert into [channel] select 'abc','6','7:35','7:45' 
insert into [channel] select 'abc','7','7:45','8:45' 

insert into [channel] select 'xyz','1','1:00','2:00' 
insert into [channel] select 'xyz','2','2:00','2:10' 
insert into [channel] select 'xyz','3','2:10','2:40' 
insert into [channel] select 'xyz','4','2:40','3:00' 
insert into [channel] select 'xyz','5','3:00','3:45' 
insert into [channel] select 'xyz','6','3:45','3:50' 

insert into [channel] select 'esk','N','2:00','3:00' 
insert into [user] select 'M',10,'esk','1:00','4:00' 
+0

ユーザが午後1時に開始すると、チャンネルレコードは午後2時から午後3時まで続き、ユーザレコードは午後4時に終了しますか? – MatBailie

+0

オーバーラップ時間範囲は、次のように定義されます。 'WHERE u.starttime c.start' – MatBailie

+0

CASE文も同様の理由で失敗します。期間は 'DATEDIFF(mi、GREATEST(u.starttime、c.start)、LEAST(u.endtime、c.end)) 'と定義されています。 – MatBailie

0

範囲が重複ソリューションを使用することができるメタデータスクリプトです他の末端WHERE a.start < b.cease AND b.start < a.start

オーバーラップの開始点は2つの開始点の最高値で、オーバーラップの停止は2つの停止点のうち最も低い値です。 a開始と停止しb
開始する前に - - b開始前にaを開始し、そして中に終了
を:テストは、彼はSQLFiddle int型覆われていたときに心に留めする

SELECT 
    CASE WHEN c.start > u.start THEN c.start ELSE u.start END overlap_start, 
    CASE WHEN c.cease < u.cease THEN c.cease ELSE u.cease END overlap_cease, 
    * 
FROM 
    user_viewing  u 
INNER JOIN 
    channel_schedule c 
     ON c.channel_id = u.channel_id 
     AND c.start  < u.cease 
     AND c.cease  > u.start 
; 

http://sqlfiddle.com/#!6/fd02e/3

重要なケースb
- ab
- 開始とb
停止した後、停止し - - b開始前にaを開始し、あなたが必要なものについて説明してきたが、いずれかの表示を提供していないb止まる

  |-------A-------> 

|--1--> |--2--> |--3--> |--4--> |--5--> 
     |----------6----------> 
関連する問題