菜鸟请教各位老鸟一问题。。
表已建好,有两列
create table TABLE_A
(codeid VARCHAR2(20) not null,
name VARCHAR2(80));
insert into TABLE_A (codeid, name)
values ('340000', '安徽省');
insert into TABLE_A (codeid, name)
values ('110000', '北京市’);
。
。
。[省的后四位是0,市的后两位是0]
像上面那样把全国的每个省,每个市,每个区县都添加到表中。
要求:先1按省排序,再2按省市排序,最后3按省市区县排序。
备注:最后一行的结果为 XX省XX市XX县
------解决方案--------------------
- SQL code
换了一种,不用判断好了!WITH a AS (SELECT '340000' a, '安徽省' b FROM dualUNION ALLSELECT '341100', '安庆市' FROM dualUNION ALLSELECT '341101', 'MMM' FROM dualUNION ALLSELECT '341102', 'AAAA' FROM dualUNION ALLSELECT '110000', '北京市' FROM dualUNION ALLSELECT '111200', '东城区' FROM dual)SELECT substr(max(sys_connect_by_path(b,',')),2)FROM ( select a,b,(CASE WHEN INSTR(a,'0000')>0 THEN 1 WHEN INSTR(a,'00')>0 THEN 2 ELSE 3 END) rn ,SUBSTR(a,1,2) aa from a)connect by prior rn=rn-1 AND PRIOR aa=aaSTART WITH rn=1group by a--考虑到另一种情况...改了一下!请TEST.--result:北京市北京市,东城区安徽省安徽省,安庆市安徽省,安庆市,MMM安徽省,安庆市,AAAA
------解决方案--------------------
select case when substr(a.codeid,-4)='0000' then
a.name||';'
when substr(a.codeid,-2)='00' then
b.name||';'||a.name ||';'
when substr(a.codeid,-2)<>'00' then
b.name||';'||c.name||';'||a.name ||';'
end
from table_a a,
table_a b, --省
table_a c --市
where b.codeid=substr(a.codeid,1,2)||'0000' and c.codeid=substr(a.codeid,1,4)||'00'
and b.codeid=substr(c.codeid,1,2)||'0000'
order by a.codeid