如何编写SQL查询以通过频道和节目播放以下数据? [关闭]
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何编写SQL查询以通过频道和节目播放以下数据? [关闭]相关的知识,希望对你有一定的参考价值。
我坚持使用查询逻辑,请找到表结构的图像。我有两个表,其中一个表是User,说明用户在哪个时间段看到的电视频道,还有另一个表我们有所有频道列表,在给定日期我们有所有播放的节目。
我需要用通道表来爆炸用户表,在用户之间,通道在给定时间内播放的节目是什么。
答案
请你检查以下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
然后重叠的开始是两次开始的最高值,并且重叠的停止是两次停止中的最低值。
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
在测试时要记住的重要案例包括在SQLFiddle中:
- a
在b
开始之前开始并停止
- a
在b
开始之前开始,并在b
结束
- a
在b
开始和停止
- a
在b
开始并在b
停止后停止
- a
在b
停止后开始并停止
- a
在b
开始之前开始并在b
停止之后停止
|-------A------->
|--1--> |--2--> |--3--> |--4--> |--5-->
|----------6---------->
以上是关于如何编写SQL查询以通过频道和节目播放以下数据? [关闭]的主要内容,如果未能解决你的问题,请参考以下文章