You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于ID关联两表,筛选满足特定日期范围条件的记录

数据表匹配查询需求

现有数据表结构及数据

Table 1

Loc_IdLabel_IdActive_DateInactive_Date
110012022/05/139999/12/31
210012018/05/202022/05/12
310012012/06/142018/05/12

Table 2

Label_IdTab2_Active_DateTab2_Inactive_Date
10012022/05/139999/12/31
10012018/05/222022/05/12
10012012/06/142018/05/12

查询需求

找出Table2中满足 Tab2_Active_Date > Table1.Active_Date 且 Tab2_Inactive_Date < Table1.Inactive_Date 的记录,同时关联对应Table1的Loc_Id。

示例:Table2中Tab2_Active_Date为2018/05/22的记录,大于Table1中Active_Date为2018/05/20的记录,符合条件。

限制条件

仅能通过Label_Id作为关联键连接两张表,不可使用日期关联,否则会导致数据不准确。

解决方案SQL

SELECT t1.Loc_Id, t2.Tab2_Active_Date, t2.Tab2_Inactive_Date
FROM Table1 t1
JOIN Table2 t2 ON t1.Label_Id = t2.Label_Id
WHERE t2.Tab2_Active_Date > t1.Active_Date
  AND t2.Tab2_Inactive_Date < t1.Inactive_Date;

预期输出

Loc_IdTab2_Active_DateTab2_Inactive_Date
22018/05/222022/05/12

内容的提问来源于stack exchange,提问作者Amit Verma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 06:20:26