![](/img/trans.png)
[英]Join two tables on a condition of ID and a date from first table is between two other dates in another table
[英]How to create a join between 2 tables where the date from the second table should be between 2 consecutive dates from the first table
我有两个表,我需要在它们之间创建一个连接:
表格1:
Session | 播放器 | 日期开始 |
---|---|---|
1234 | 一种 | 2022 年 9 月 20 日 13:25 |
1234 | 一种 | 2022 年 9 月 20 日 14:23 |
4532 | 乙 | 2022 年 9 月 20 日 14:43 |
4532 | 乙 | 2022 年 9 月 20 日 14:46 |
1234 | 一种 | 2022 年 9 月 20 日 14:50 |
1456 | 一种 | 2022 年 9 月 20 日 16:24 |
1456 | 一种 | 2022 年 9 月 20 日 16:48 |
表 2:
Session | 播放器 | 结束日期 |
---|---|---|
1234 | 一种 | 2022 年 9 月 20 日 13:28 |
1234 | 一种 | 2022 年 9 月 20 日 14:30 |
4532 | 乙 | 2022 年 9 月 20 日 14:45 |
4532 | 乙 | 2022 年 9 月 20 日 14:49 |
1234 | 一种 | 2022 年 9 月 20 日 14:52 |
1456 | 一种 | 2022 年 9 月 20 日 16:29 |
1456 | 一种 | 2022 年 9 月 20 日 16:49 |
对于相同的 session 和播放器,结束日期应介于 2 个连续的开始日期之间
Session | 播放器 | 日期开始 | 结束日期 |
---|---|---|---|
1234 | 一种 | 2022 年 9 月 20 日 13:25 | 2022 年 9 月 20 日 13:28 |
1234 | 一种 | 2022 年 9 月 20 日 14:23 | 2022 年 9 月 20 日 14:30 |
4532 | 乙 | 2022 年 9 月 20 日 14:43 | 2022 年 9 月 20 日 14:45 |
4532 | 乙 | 2022 年 9 月 20 日 14:46 | 2022 年 9 月 20 日 14:49 |
1234 | 一种 | 2022 年 9 月 20 日 14:50 | 2022 年 9 月 20 日 14:52 |
1456 | 一种 | 2022 年 9 月 20 日 16:24 | 2022 年 9 月 20 日 16:29 |
1456 | 一种 | 2022 年 9 月 20 日 16:48 | 2022 年 9 月 20 日 16:49 |
假设您可以在没有另一行时为上边界使用任意高的值,您可以使用LEAD
和默认值来创建您的范围,然后使用>=
和<
/ <=
JOIN
(取决于您需要什么) ON
中的逻辑:
WITH T1 AS (
SELECT Session,
Player,
DateStart,
LEAD(DateStart,1,'99991231') OVER (PARTITION BY Session, Player ORDER BY DateStart) AS NextDateStart
FROM dbo.Table1 T1)
SELECT T1.Session,
T1.Player,
T1.DateStart,
T2.DateEnd
FROM T1
JOIN dbo.Table2 T2 ON T1.Session = T2.Session
AND T1.Player = T2.Player
AND T1.DateStart <= T2.DateEnd
AND T1.NextDateStart >= T2.DateEnd;
我认为这个查询可以帮助你,试一试。
with table_1 (Session,Player,Datestart)
as
(
Select '1234','A','2022-09-20 13:25:00.000'
union all Select '1234','A','2022-09-20 14:23:00.000'
union all Select '4532','B','2022-09-20 14:43:00.000'
union all Select '4532','B','2022-09-20 14:46:00.000'
union all Select '1234','A','2022-09-20 14:50:00.000'
union all Select '1456','A','2022-09-20 16:24:00.000'
union all Select '1456','A','2022-09-20 16:48:00.000'
),
table_2 (Session,Player,Dateend)
as
(
Select '1234','A','2022-09-20 13:28:00.000'
union all Select '1234','A','2022-09-20 14:30:00.000'
union all Select '4532','B','2022-09-20 14:45:00.000'
union all Select '4532','B','2022-09-20 14:49:00.000'
union all Select '1234','A','2022-09-20 14:52:00.000'
union all Select '1456','A','2022-09-20 16:26:00.000'
union all Select '1456','A','2022-09-20 16:49:00.000'
)
Select
x.Session,
x.Player,
x.Datestart,
y.Dateend
from (
Select
Session,
Player,
Datestart,
ROW_NUMBER() OVER (PARTITION BY Session,Player ORDER BY Datestart) a
FROM
table_1
) x
inner join
(
Select
Session as Session_2,
Player as PLayer_2,
Dateend,
ROW_NUMBER() OVER (PARTITION BY Session,Player ORDER BY Dateend) b
FROM
table_2
) as y
on
x.Session = y.Session_2 and
x.Player = y.PLayer_2 and
x.a = y.b
order by
x.Datestart
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.