--已知结果集:
SQL> select * from area;
ID AREAID AREANAME AREAPARENT AREASTATUS
---------- ---------- ------------------------------ ---------- ----------
1 110000 北京市 0 1
35 110100 市辖区 110000 1
36 110200 县 110000 1
380 110101 东城区 110100 1
381 110102 西城区 110100 1
382 110103 崇文区 110100 1
387 110108 海淀区 110100 1
395 110117 平谷区 110100 1
396 110228 密云县 110200 1
397 110229 延庆县 110200 1
10 rows selected--建表脚本如下:
create table AREA
(
ID NUMBER not null, --PK
AREAID NUMBER not null, --UK
AREANAME VARCHAR2(80) not null,
AREAPARENT NUMBER not null,
AREASTATUS CHAR(1) default 1 not null
);--模拟数据如下:
insert into area values (1 ,110000,'北京市 ',0 ,'1');
insert into area values (35 ,110100,'市辖区 ',110000,'1');
insert into area values (36 ,110200,'县 ',110000,'1');
insert into area values (380,110101,'东城区 ',110100,'1');
insert into area values (381,110102,'西城区 ',110100,'1');
insert into area values (382,110103,'崇文区 ',110100,'1');
insert into area values (387,110108,'海淀区 ',110100,'1');
insert into area values (395,110117,'平谷区 ',110100,'1');
insert into area values (396,110228,'密云县 ',110200,'1');
insert into area values (397,110229,'延庆县 ',110200,'1');--希望得到如下结果:
SQL> select ..... from area where ..... areaid=110108
col
----------------------------
北京市 市辖区 海淀区SQL> select ..... from area where ..... areaid=110228
col
----------------------------
北京市 县 密云县
我自己写的:SQL> select replace(wm_concat(areaname),',','') from area
2 start with areaid = 110108
3 connect by areaid = prior areaparent;
REPLACE(WM_CONCAT(AREANAME),',
--------------------------------------------------------------------------------
海淀区 市辖区 北京市
但结果是要求: 北京市 市辖区 海淀区 请高手们帮忙解答下!
使用lag函数或自连接或层次查询均可!
解决方案 »
- 关于导入表空间的问题。
- 关于把java获取的当前时间插入oralc的问题
- The Network Adapter could not establish the connection
- 谁能给个ORACLE下创建用户(分配权限)和创建数据库的脚本啊?
- 请问高手 --配置监听程序
- 请教rman直接备份到磁带问题!
- !!!有问题!ORACLE9I 一个简单表,数据量在1000万以上
- 为什么我在用光标去数据的时候有时候会少取一条啊?啊~~~哪位老大告诉我啊
- Oralce中有将字符串'FF'转换成数字255的函数吗?
- 请问可以将oracle的启动时间控制在10秒以内吗?
- 在安装oracle数据库软件时,遇到的与数据库监听程序有关的问题
- 4个abcd的问题??
from area a, area b, area c
where a.areaid = b.areaparent
and b.areaid = c.areaparent
and c.areaid = '110108'
SQL> select replace(sys_connect_by_path(areaname,'->'),'->'),areaid from area
2 where areaid=110108 and areastatus=1
3 start with areaparent =0 and areastatus=1
4 connect by prior areaid=areaparent;
REPLACE(SYS_CONNECT_BY_PATH(AR AREAID
-------------------------------------------------------------------------------- ----------
北京市 市辖区 海淀区 110108
select replace(wm_concat(areaname),',','') from
(select areaname from area
start with areaid = 110108
connect by areaid = prior areaparent
order by level desc);