- 爱易网页
-
Oracle教程
- oracle 结果集排列组合解决方案
日期:2014-05-16 浏览次数:21397 次
oracle 结果集排列组合
有这样一个需求,原始数据为:
NAME GROUP
n1 null
n2 g1
n3 g1
n4 g2
n5 g2
需求,将名称按组进行排列组合,中间用:分隔,要求结果为
RESULT
n1:n2:n4
n1:n2:n5
n1:n3:n4
n1:n3:n5
也就是,每组最多有两个,每多一组,最后的结果集数量翻倍。求高手指点该怎么做,要用sql实现
------解决方案--------------------
没看懂,如果再多个g3会怎样
------解决方案--------------------
想了半天,能想出来的只有通过过程。
更好的思路请看楼下讲解。
------解决方案--------------------
这个题目还是有点难度的,你可以去itpub问问。。
------解决方案--------------------
http://www.itpub.net/thread-763065-1-1.html
看看这个:
SQL>
SQL> select replace (a.combo, '#') as "组合"
2 from
3 (select id,sys_connect_by_path (value, '#')
------解决方案--------------------
'#' combo
4 from (select 1 as id,value,1 as ctrl from users)
5 connect by prior id = id and value > prior value ) a,
6 (select 1 as id,value,1 as ctrl from users) b
7 where b.id = a.id and instr (a.combo, '#'
------解决方案--------------------
b.value
------解决方案--------------------
'#') > 0
8 group by a.id, a.combo
9 having sum (b.ctrl) = 3;
组合
--------------------------------------------------------------------------------
acd
abc
abd
bcd
------解决方案--------------------
冲你这句谢谢,研究了下。很繁琐的实现方式
select * from (
select case when b ='g2' then (
select g from
(
select g,rownum from (select case when b ='g1' then (select a from bb where b is null)
------解决方案--------------------
':'
------解决方案--------------------
a end g from bb) f
where g is not null
) g where rownum=1)
------解决方案--------------------
':'
------解决方案--------------------
a end ff
from bb
union
select case when b ='g2' then (
select g from
(
select g,rownum dd from (select case when b ='g1' then (select a from bb where b is null)
------解决方案--------------------
':'