晕~~~~~~~~~我也在找这样的SQL啊.那位大虾浮下水啊tree_code bg_order num
0000100003 c22-bg-1 1
0000200002 c22-bg-1 2
000030000200001 c22-bg-2 3
0000200002 c22-bg-2 4要的结果
tree_code bg_order
0000100003 c22-bg-1
0000200002 c22-bg-1, c22-bg-2
000030000200001 c22-bg-2
0000100003 c22-bg-1 1
0000200002 c22-bg-1 2
000030000200001 c22-bg-2 3
0000200002 c22-bg-2 4要的结果
tree_code bg_order
0000100003 c22-bg-1
0000200002 c22-bg-1, c22-bg-2
000030000200001 c22-bg-2
解决方案 »
- 怎么只取兩位小數
- ■■■■■■■如何读取oracle中的blob字段
- 利用DBLINK创建表的问题
- 数据库里foreign key......是什么意思
- oracle公司的笔试题,请求各位高手的帮助
- 我想连接着三个表使用以上条件在oralce中用聚合函数和汇总函数求20060126当天的总订单金额。请指教。谢谢!!!
- 通过 Microsoft ODBC for Oracle 连接远程主机的Oracle数据库,怎么才能实现?
- 用sql语句查询数据表中有哪些字段是什么语句?
- 在ORACLE DBA STUDIO 一条都看不到?(初学者)
- 急急,我编写的一个过程的状态为何总是“invalid”啊,在sqlplus里call时总是执行不了,为何?
- 高手请指教,如何将mssqlserver2000里的一张表里的记录全部导入到oracle9i里的一张表里?
- 数据导入问题!
2. 用PL/SQL 的动态SQL实现
[A]1、固定列数的行列转换
如
student subject grade
---------------------------
student1 语文 80
student1 数学 70
student1 英语 60
student2 语文 90
student2 数学 80
student2 英语 100
……
转换为
语文 数学 英语
student1 80 70 60
student2 90 80 100
……
语句如下:
select student,sum(decode(subject,'语文', grade,null)) "语文",
sum(decode(subject,'数学', grade,null)) "数学",
sum(decode(subject,'英语', grade,null)) "英语"
from table
group by student
大概是类似下列的情形:
SQL> select no,q from test
2 /NO Q
---------- ------------------------------
001 n1
001 n2
001 n3
001 n4
001 n5
002 m1
003 t1
003 t2
003 t3
003 t4
003 t5
003 t612 rows selected最后要得到类似于如下的结果:
001 n1;n2;n3;n4;n5
002 m1
003 t1;t2;t3;t4;t5;t6 通常大家都认为这类问题无法用一句SQL解决,本来我也这么认为,可是今天无意中突然有了灵感,原来是可以这么做的:
前几天有人提到过sys_connect_by_path的用法,我想这里是不是也能用到这个方法,如果能做到的话,不用函数或存贮过程也可以做到了;要用到sys_connect_by_path,首先要自己构建树型的结构,并且树的每个分支都是单根的,例如1-〉2-〉3-〉4,不会存在1-〉2,1-〉3的情况;
我是这么构建树,很简单的,看下面的结果就会知道了:
SQL> select no,q,rn,lead(rn) over(partition by no order by rn) rn1
2 from (select no,q,row_number() over(order by no,q desc) rn from test)
3 /NO Q RN RN1
---------- ------------------------------ ---------- ----------
001 n5 1 2
001 n4 2 3
001 n3 3 4
001 n2 4 5
001 n1 5
002 m1 6
003 t6 7 8
003 t5 8 9
003 t4 9 10
003 t3 10 11
003 t2 11 12
003 t1 12 12 rows selected有了这个树型的结构,接下来的事就好办了,只要取出拥有全路径的那个path,问题就解决了,先看no=‘001’的分组:
select no,sys_connect_by_path(q,';') result from
(select no,q,rn,lead(rn) over(partition by no order by rn) rn1
from (select no,q,row_number() over(order by no,q desc) rn from test)
)
start with no = '001' and rn1 is null connect by rn1 = prior rn
SQL>
6 /NO RESULT
---------- --------------------------------------------------------------------------------
001 ;n1
001 ;n1;n2
001 ;n1;n2;n3
001 ;n1;n2;n3;n4
001 ;n1;n2;n3;n4;n5上面结果的最后1条就是我们要得结果了
要得到每组的结果,可以下面这样select t.*,
(
select max(sys_connect_by_path(q,';')) result from
(select no,q,rn,lead(rn) over(partition by no order by rn) rn1
from (select no,q,row_number() over(order by no,q desc) rn from test)
)
start with no = t.no and rn1 is null connect by rn1 = prior rn
) value
from (select distinct no from test) tSQL>
10 /NO VALUE
---------- --------------------------------------------------------------------------------
001 ;n1;n2;n3;n4;n5
002 ;m1
003 ;t1;t2;t3;t4;t5;t6对上面结果稍加处理就可以了,希望对大家有帮助:)
from (select rownum m,rownum-1 n,A
from table
) connect
by prior m=n start with n=0
我在其他的帖里看到这个.但运行出错.说什么文件通道关闭.我用的也是9i啊.
leborety(那只螃蟹) 搞定了
^_^