- SQL code
--1. 构建测试表test id 范围为 0 -- 178 SQL> create table test (id) as select trunc(dbms_random.value(0,179)) from dual connect by level < 10000 ;Table created.--2. 构建范围表range_ SQL> create table range_ ( begin_ number , end_ number , id number) ;Table created.SQL> insert into range_ values (0,27,1) ;1 row created.SQL> insert into range_ values (27,48,2) ;1 row created.SQL> insert into range_ values (48,96,3) ;1 row created.SQL> insert into range_ values (96,111,4) ;1 row created.SQL> insert into range_ values (111,178,5) ;1 row created.SQL> commit ;Commit complete.SQL> select * from range_ order by id ; BEGIN_ END_ ID---------- ---------- ---------- 0 27 1 27 48 2 48 96 3 96 111 4 111 178 5--目标 根据 范围表 range_ 的列 begin_ end_ 统计 test 表中的数据分布情况 --目标结果集举例: BEGIN_ END_ ID COUNT---------- ---------- ---------- ------- 0 27 1 2000 27 48 2 1999 48 96 3 2001 96 111 4 1888 111 178 5 2111
------解决方案--------------------
缺少这部分
SELECT Count(*) FROM test WHERE id=0;
------解决方案--------------------
确实缺少id=0的情况
------解决方案--------------------
------解决方案--------------------
- SQL code
select r.*, (select count(*) from test t where (t.id > r.begin_ and t.id <= r.end_) --不是从0开始的计数 or (t.id >= r.begin_ and t.id <= r.end_ and r.begin_=0)--从0开始的计数 ) as count from range_ r;