私はクエリロジックがついていません。テーブル構造のイメージを見つけてください。私は1つのテーブルが2つのテーブルを持っていて、どのユーザーがどのテレビのチャンネルを視聴したのか、そこにはすべてのチャンネルのリストがあるテーブルがあります。 。チャネルとプログラムによって次のデータを表示するSQLクエリを作成するにはどうすればよいですか?
私はチャネルテーブルを使用してユーザテーブルを爆発させる必要があります。チャンネル間では、指定された時間にチャネルが再生されます。
私はクエリロジックがついていません。テーブル構造のイメージを見つけてください。私は1つのテーブルが2つのテーブルを持っていて、どのユーザーがどのテレビのチャンネルを視聴したのか、そこにはすべてのチャンネルのリストがあるテーブルがあります。 。チャネルとプログラムによって次のデータを表示するSQLクエリを作成するにはどうすればよいですか?
私はチャネルテーブルを使用してユーザテーブルを爆発させる必要があります。チャンネル間では、指定された時間にチャネルが再生されます。
あなたはここでは、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'
範囲が重複ソリューションを使用することができるメタデータスクリプトです他の末端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
- a
b
- 開始とb
が
停止した後、停止し - - b
開始前にa
を開始し、あなたが必要なものについて説明してきたが、いずれかの表示を提供していないb
止まる
|-------A------->
|--1--> |--2--> |--3--> |--4--> |--5-->
|----------6---------->
後に停止し
b
とb
はを停止した後に停止し中の開始あなたが作った努力の –
期待される結果は何ですか? _explodeはあなたのユーザーテーブルを意味しますか? – Sami
DDLとサンプル・データをSQLスクリプトとして提供した方が良いでしょう。 – Eralper