专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Oracle技术

oracle 结果集排列组合解决方案

发布时间:2010-05-20 14:01:29 文章来源:www.iduyao.cn 采编人员:星星草
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)
------解决方案--------------------
':'
------解决方案--------------------
 a end  g from bb) f
 where g is not null
) g  where dd=2)
------解决方案--------------------
':'
------解决方案--------------------
 a end ff

   from bb) ggg where ff is not null
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: