获取分层查询的每个级别的计数/总计我有这样的时间。我正在尝试使用递归关系(分层)编写查询(使用Oracle),并获取存储在树中每个节点上下的另一个表中的记录总数。另一个表只有与叶节点相关的记录。但是,我想获得树中每个节点上下的总数。例如,假设我有两张桌子。 DIRS包含目录名称和递归关系鉴别的目录结构,文件和文件包含一个外键DIRS指示文件所在的目录文件信息:使用CONNECT BY
DIRS
====
DIR_ID
PARENT_DIR_ID
DIR_NAME
FILES
=====
FILE_ID
FILE_NAME
DIR_ID
FILE_SIZE
如果DIRS包含:
DIR_ID PARENT_DIR_ID DIR_NAME
====== ============= ========
1 ROOT
2 1 DIR1_1
3 1 DIR1_2
4 2 DIR2_1
5 2 DIR2_2
和FILES包含
FILE_ID FILE_NAME DIR_ID FILE_SIZE
======= ========= ====== =========
1 test1.txt 5 100
2 test2.txt 5 200
3 test5.txt 5 50
4 test3.txt 3 300
5 test4.txt 3 300
6 test6.txt 4 100
我想,在票数与文件的数目以及返回或下面的每个节点的路径查询君主国。基本上是文件数量的汇总。所以查询结果将类似于:
Path File_Count
===== ===========
/ROOT 6
/ROOT/DIR1_1 4
/ROOT/DIR1_1/DIR2_1 1
/ROOT/DIR1_1/DIR2_2 3
/ROOT/DIR1_2 2
UPDATE SQL脚本来创建示例数据表,以匹配上面:
create table DIRS (dir_id number(38) primary key
, parent_dir_id number(38) null references DIRS(dir_id)
, dir_name varchar2(128) not null);
create table FILES (file_id number(38) primary key
, file_name varchar2(128) not null
, dir_id number(38) not null references DIRS(dir_id)
, file_size number not null
, unique (dir_id, file_name));
insert into DIRS
select 1, null, 'ROOT' from dual
union all select 2, 1, 'DIR1_1' from dual
union all select 3, 1, 'DIR1_2' from dual
union all select 4, 2, 'DIR2_1' from dual
union all select 5, 2, 'DIR2_2' from dual;
insert into files
select 1, 'test1.txt', 5, 100 from dual
union all select 2, 'test2.txt', 5, 200 from dual
union all select 3, 'test5.txt', 5, 50 from dual
union all select 4, 'test3.txt', 3, 300 from dual
union all select 5, 'test4.txt', 3, 300 from dual
union all select 6, 'test6.txt', 4, 100 from dual;
commit;
哪个Oracle的版本? 11g R2添加了递归子查询因子分解,这可能比'connect by'提供更清晰的解决方案。 –
是11g R2。将研究“子查询因子”。不熟悉这一点。 – GregH
看起来像在Oracle 9.2中添加了子查询因子 – GregH