我有一个显示类型的表(未显示所有列)。typeid就是PK。有一些不应该存在的日期重叠。我想从我的查询中删除这些。
Custid typeid start_dt end_dt
101 352 3/28/2017 1/16/2019
101 353 1/15/2018 1/15/2019
数据显示如下(重叠示例)。
Custid typeid start_dt end_dt overlap
101 352 3/28/2017 1/16/2019 1
101 353 1/15/2018 1/15/2019 1
201 426 1/1/2019 12/31/9999 0
SQL。(索引在所有上述表格列上)
select *
from (select
Custid,
typeid,
start_dt,
case
when end_dt > lead (start_dt) over (partition by custid order by start_dt) then 1
when start_dt < lag (end_dt) over (partition by custid order by start_dt) then 1
else 0
end overlap)
where overlap = 0
对 case 表达式的过滤会减慢查询返回结果的速度。是否有任何提示/技巧可以提高查询性能?