使用 Oracle SQL。有两个表:第一个包含数字范围,第二个包含数字。我需要的结果是第一个表加上第二个表中的数字,这些数字不在第一个表的数字范围内。
table ranges
LOW_RANGE HIGH_RANGE
1000000000000000000 1000000099999999999
4253525250000000000 4253525259999999999
4352000000000000000 4352999999999999999
table numbers
NUMBER LENGTH (implies that the rest of the numbers are 0-9 range)
100000000000 12(max)
100000000051 12
425252525100 10
435252525000 9
425353000000 6
797689600000 9
needed result:
LOW_RANGE HIGH_RANGE
100000000000 100000009999 (ranges)
425252525000 425252525999 (ranges)
435200000000 435299999999 (ranges)
425353000000 425353999999
797689600000 797689600999
我有这个解决方案:
SELECT
SUBSTR(ranges.LOW_RANGE,1,9)||'000'
,SUBSTR(ranges.HIGH_RANGE,1,9)||'999'
FROM
ranges
UNION ALL
SELECT
numbers.NUMBER
,RPAD(SUBSTR(numbers.NUMBER,1,numbers.NUMBERLENGTH),12,'9')
FROM
numbers b
WHERE b.NUMBER NOT IN
(
SELECT b.NUMBER
FROM ranges a
WHERE b.NUMBER BETWEEN SUBSTR(a.LOW_RANGE,1,9)||'000' AND SUBSTR(a.HIGH_RANGE,1,9)||'999'
)
这很有效,但对于大数据集来说非常慢。还有其他更快的解决方案吗?
编辑:修改后的问题