DECLARE @tbl TABLE
(
ID INT ,
FROM_VALUE INT ,
TO_VALUE INT
)
INSERT @tbl
( ID, FROM_VALUE, TO_VALUE )
VALUES ( 1, 10, -- A - int
30 -- B - int
)
INSERT @tbl
( ID, FROM_VALUE, TO_VALUE )
VALUES ( 2, 20, -- A - int
40 -- B - int
)
INSERT @tbl
( ID, FROM_VALUE, TO_VALUE )
VALUES ( 3, 41, -- A - int
60 -- B - int
)
SELECT
DISTINCT
R.*
FROM @tbl R
INNER JOIN @tbl I ON ( ( R.FROM_VALUE <= I.TO_VALUE
AND I.FROM_VALUE <= R.TO_VALUE
)
AND NOT ( R.FROM_VALUE = I.FROM_VALUE
AND I.TO_VALUE = R.TO_VALUE
)
)
ORDER BY ID
常用SQL整理 - 查找存在交集的数据
最新推荐文章于 2024-09-07 16:50:35 发布