如何在Oracle中找到两个大小范围之间缺少的大小范围?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在Oracle中找到两个大小范围之间缺少的大小范围?相关的知识,希望对你有一定的参考价值。
我想验证两个尺寸范围之间缺少的尺寸范围。例如,现在我有一个名为AQL的表,该表包含以下列:
ID TYPE SIZE_FROM SIZE_TO
-- ---------- --------- -------
68 NORMAL 2 150
69 NORMAL 501 1200
70 REDUCED 1201 3200
71 REDUCED 151 500
我需要找出缺少的范围是:
TYPE SIZE_FROM SIZE_TO
--------- --------- -------
NORMAL 151 500
REDUCED 501 1200
我已经处理了重叠部分,那么该怎么做才能选择以下缺失范围?
答案
有关示例数据,请参阅是否有帮助(阅读代码中的注释):
SQL> with
2 aql (id, type, size_from, size_to) as
3 -- Your current data
4 (select 68, 'NORMAL' , 2, 150 from dual union all
5 select 69, 'NORMAL' , 501, 1200 from dual union all
6 select 70, 'REDUCED', 1201, 3200 from dual union all
7 select 71, 'REDUCED', 151, 500 from dual
8 ),
9 existing_sizes as
10 -- list all existing sizes, one-by-one, per type
11 (select type, size_from + column_value - 1 csize
12 from aql cross join table(cast(multiset(select level from dual
13 connect by level <= size_to - size_from + 1
14 ) as sys.odcinumberlist))
15 ),
16 minmax as
17 -- find MIN and MAX size valze per type - will be used in ALLSIZES CTE
18 (select type, min(size_from) size_from, max(size_to) size_to
19 from aql
20 group by type
21 ),
22 allsizes as
23 -- list all sizes (per type) between MIN and MAX value
24 (select type, size_from + column_value - 1 csize
25 from minmax cross join table(cast(multiset(select level from dual
26 connect by level <= size_to - size_from + 1
27 ) as sys.odcinumberlist))
28 ),
29 missing_sizes as
30 -- missing sizes can be found with MINUS set operator
31 (select a.type, a.csize from allsizes a
32 minus
33 select e.type, e.csize from existing_sizes e
34 )
35 -- the final result
36 select type, min(csize) size_from, max(csize) size_to
37 from missing_sizes
38 group by type;
TYPE SIZE_FROM SIZE_TO
------- ---------- ----------
NORMAL 151 500
REDUCED 501 1200
SQL>
但是,如果涉及更多的差距,这将失败(但这不是您的示例数据所建议的)。
以上是关于如何在Oracle中找到两个大小范围之间缺少的大小范围?的主要内容,如果未能解决你的问题,请参考以下文章
如何在python中找到用户的偏好向量和项目描述表(大小不同的矩阵)之间的相似距离?
如何在 Oracle SQL 的 Where 语句中添加大小写
如何在 pl/sql (oracle 9i) 中查找数据类型的大小?