SQL高手在哪里?100分散了

shxng 2014-04-22 05:37:09
例如表中有两列C1,C2 ,内容如下:
C1 C2
A 101,103
B 102,103,104
C 101,102,103
D 101,103,104
求结果如下的SQL语句:
ACD 101,103
BC 102,103
BD 103,104
...全文
316 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
流浪川 2014-04-23
  • 打赏
  • 举报
回复
引用 22 楼 wildwave 的回复:
[quote=引用 21 楼 hidanger521 的回复:] [quote=引用 19 楼 hidanger521 的回复:] [quote=引用 18 楼 shxng 的回复:] 如果表内容是下面的结构,会不会简单些: C1 C2 A 101 A 103 B 102 B 103 B 104 C 101 C 102 C 103 D 101 D 103 D 104
是,这样的话,我就不用帮你拆分了,相当于剩了一步,效率也会提高很多,直接自关联就可以了[/quote]
select replace(wmsys.wm_concat(distinct t1.c1),',','') c1,
  (case when t1.c2 > t2.c2 then t2.c2 else t1.c2 end) || ',' || 
  (case when t1.c2 > t2.c2 then t1.c2 else t2.c2 end)  c2
  from tab t1,tab t2
 where t1.c1 = t2.c1
   and t1.c2 <> t2.c2
 group by (case when t1.c2 > t2.c2 then t2.c2 else t1.c2 end),
          (case when t1.c2 > t2.c2 then t1.c2 else t2.c2 end) 
   having count(1)>2
[/quote] 思维不错,明日之星啊[/quote] 大大啊,趁有空瞎琢磨的。。
流浪川 2014-04-23
  • 打赏
  • 举报
回复
引用 23 楼 shxng 的回复:
还是不拆分的效率高,速度快,还有一个问题,如果列C2内容是字符型,这个语句还能执行不?
不影响,只要别包含,等特殊字符参与运算就行
shxng 2014-04-23
  • 打赏
  • 举报
回复
应该可以的,我试了,多谢!结贴散分
shxng 2014-04-23
  • 打赏
  • 举报
回复
还是不拆分的效率高,速度快,还有一个问题,如果列C2内容是字符型,这个语句还能执行不?
小灰狼W 2014-04-23
  • 打赏
  • 举报
回复
引用 21 楼 hidanger521 的回复:
[quote=引用 19 楼 hidanger521 的回复:] [quote=引用 18 楼 shxng 的回复:] 如果表内容是下面的结构,会不会简单些: C1 C2 A 101 A 103 B 102 B 103 B 104 C 101 C 102 C 103 D 101 D 103 D 104
是,这样的话,我就不用帮你拆分了,相当于剩了一步,效率也会提高很多,直接自关联就可以了[/quote]
select replace(wmsys.wm_concat(distinct t1.c1),',','') c1,
  (case when t1.c2 > t2.c2 then t2.c2 else t1.c2 end) || ',' || 
  (case when t1.c2 > t2.c2 then t1.c2 else t2.c2 end)  c2
  from tab t1,tab t2
 where t1.c1 = t2.c1
   and t1.c2 <> t2.c2
 group by (case when t1.c2 > t2.c2 then t2.c2 else t1.c2 end),
          (case when t1.c2 > t2.c2 then t1.c2 else t2.c2 end) 
   having count(1)>2
[/quote] 思维不错,明日之星啊
流浪川 2014-04-23
  • 打赏
  • 举报
回复
引用 19 楼 hidanger521 的回复:
[quote=引用 18 楼 shxng 的回复:] 如果表内容是下面的结构,会不会简单些: C1 C2 A 101 A 103 B 102 B 103 B 104 C 101 C 102 C 103 D 101 D 103 D 104
是,这样的话,我就不用帮你拆分了,相当于剩了一步,效率也会提高很多,直接自关联就可以了[/quote]
select replace(wmsys.wm_concat(distinct t1.c1),',','') c1,
  (case when t1.c2 > t2.c2 then t2.c2 else t1.c2 end) || ',' || 
  (case when t1.c2 > t2.c2 then t1.c2 else t2.c2 end)  c2
  from tab t1,tab t2
 where t1.c1 = t2.c1
   and t1.c2 <> t2.c2
 group by (case when t1.c2 > t2.c2 then t2.c2 else t1.c2 end),
          (case when t1.c2 > t2.c2 then t1.c2 else t2.c2 end) 
   having count(1)>2
shxng 2014-04-23
  • 打赏
  • 举报
回复
那你帮我按这个写一下吧
流浪川 2014-04-23
  • 打赏
  • 举报
回复
引用 18 楼 shxng 的回复:
如果表内容是下面的结构,会不会简单些: C1 C2 A 101 A 103 B 102 B 103 B 104 C 101 C 102 C 103 D 101 D 103 D 104
是,这样的话,我就不用帮你拆分了,相当于剩了一步,效率也会提高很多,直接自关联就可以了
shxng 2014-04-23
  • 打赏
  • 举报
回复
如果表内容是下面的结构,会不会简单些: C1 C2 A 101 A 103 B 102 B 103 B 104 C 101 C 102 C 103 D 101 D 103 D 104
小灰狼W 2014-04-23
  • 打赏
  • 举报
回复
引用 16 楼 hidanger521 的回复:
[quote=引用 15 楼 wildwave 的回复:]
select wm_concat(distinct c1_1) c1, substr(factor, 2) factor
  from (select c1_1, c1_2, c2_1, sys_connect_by_path(keyword, ',') factor
          from (select a.c1 c1_1,
                       a.c2 c2_1,
                       b.c1 c1_2,
                       b.keyword,
                       row_number() over(partition by b.c1, a.c1 order by b.keyword) rn
                  from tab1 a,
                       (select distinct c1,
                                        REGEXP_SUBSTR(C2, '[^,]+', 1, LEVEL) keyword
                          from tab1
                        connect by level <=
                                   LENGTH(REGEXP_REPLACE(C2, '[^,]+')) + 1) b
                 where a.c1 != b.c1
                   and instr(',' || a.c2 || ',', ',' || b.keyword || ',') > 0)
         where level >= 2
         start with rn = 1
        connect by prior c1_1 = c1_1
               and prior c1_2 = c1_2
               and prior rn = rn - 1)
 group by factor
有点问题,大大,他要的是两两组合,如果上面的案例2个字符串自检有三个相同字符的话,你这个就不是两两组合了。。[/quote] 嗯,我是按2次以上来做的
流浪川 2014-04-23
  • 打赏
  • 举报
回复
引用 15 楼 wildwave 的回复:
select wm_concat(distinct c1_1) c1, substr(factor, 2) factor
  from (select c1_1, c1_2, c2_1, sys_connect_by_path(keyword, ',') factor
          from (select a.c1 c1_1,
                       a.c2 c2_1,
                       b.c1 c1_2,
                       b.keyword,
                       row_number() over(partition by b.c1, a.c1 order by b.keyword) rn
                  from tab1 a,
                       (select distinct c1,
                                        REGEXP_SUBSTR(C2, '[^,]+', 1, LEVEL) keyword
                          from tab1
                        connect by level <=
                                   LENGTH(REGEXP_REPLACE(C2, '[^,]+')) + 1) b
                 where a.c1 != b.c1
                   and instr(',' || a.c2 || ',', ',' || b.keyword || ',') > 0)
         where level >= 2
         start with rn = 1
        connect by prior c1_1 = c1_1
               and prior c1_2 = c1_2
               and prior rn = rn - 1)
 group by factor
有点问题,大大,他要的是两两组合,如果上面的案例2个字符串自检有三个相同字符的话,你这个就不是两两组合了。。
小灰狼W 2014-04-23
  • 打赏
  • 举报
回复
select wm_concat(distinct c1_1) c1, substr(factor, 2) factor
  from (select c1_1, c1_2, c2_1, sys_connect_by_path(keyword, ',') factor
          from (select a.c1 c1_1,
                       a.c2 c2_1,
                       b.c1 c1_2,
                       b.keyword,
                       row_number() over(partition by b.c1, a.c1 order by b.keyword) rn
                  from tab1 a,
                       (select distinct c1,
                                        REGEXP_SUBSTR(C2, '[^,]+', 1, LEVEL) keyword
                          from tab1
                        connect by level <=
                                   LENGTH(REGEXP_REPLACE(C2, '[^,]+')) + 1) b
                 where a.c1 != b.c1
                   and instr(',' || a.c2 || ',', ',' || b.keyword || ',') > 0)
         where level >= 2
         start with rn = 1
        connect by prior c1_1 = c1_1
               and prior c1_2 = c1_2
               and prior rn = rn - 1)
 group by factor
流浪川 2014-04-23
  • 打赏
  • 举报
回复
引用 11 楼 shxng 的回复:
C2是不定个数的
WITH tab1 AS(
SELECT 'A' C1, '101,103' C2 FROM DUAL UNION ALL
SELECT 'B', '102,103,104'  FROM DUAL UNION ALL
SELECT 'C', '101,102,103'  FROM DUAL UNION ALL
SELECT 'D', '101,103,104' FROM DUAL)
select replace(wmsys.wm_concat(distinct t1.c1),',','') c1,
  (case when t1.c2 > t2.c2 then t2.c2 else t1.c2 end) || ',' || 
  (case when t1.c2 > t2.c2 then t1.c2 else t2.c2 end)  c2
  from (SELECT DISTINCT C1, REGEXP_SUBSTR(C2, '[^,]+', 1, LEVEL) C2
          FROM TAB1
        CONNECT BY LEVEL <= LENGTH(REGEXP_REPLACE(C2, '[^,]+')) + 1) t1,
       (SELECT DISTINCT C1, REGEXP_SUBSTR(C2, '[^,]+', 1, LEVEL) C2
          FROM TAB1
        CONNECT BY LEVEL <= LENGTH(REGEXP_REPLACE(C2, '[^,]+')) + 1) t2
 where t1.c1 = t2.c1
   and t1.c2 <> t2.c2
 group by (case when t1.c2 > t2.c2 then t2.c2 else t1.c2 end),
          (case when t1.c2 > t2.c2 then t1.c2 else t2.c2 end) 
   having count(1)>2
帮你写完得了。。应该和你要的效果是一样的
流浪川 2014-04-23
  • 打赏
  • 举报
回复
引用 9 楼 shxng 的回复:
oracle有没有组合函数?
我不知道有这样的函数,自己给你写了一个,希望能帮助你
流浪川 2014-04-23
  • 打赏
  • 举报
回复
引用 11 楼 shxng 的回复:
C2是不定个数的
WITH tab1 AS(
SELECT 'A' C1, '101,103' C2 FROM DUAL UNION ALL
SELECT 'B', '102,103,104'  FROM DUAL UNION ALL
SELECT 'C', '101,102,103'  FROM DUAL UNION ALL
SELECT 'D', '101,103,104' FROM DUAL)
select distinct t1.c1,
                (case
                  when t1.c2 > t2.c2 then
                   t2.c2
                  else
                   t1.c2
                end) c1,
                (case
                  when t1.c2 > t2.c2 then
                   t1.c2
                  else
                   t2.c2
                end) c2
  from (SELECT DISTINCT C1, REGEXP_SUBSTR(C2, '[^,]+', 1, LEVEL) C2
          FROM TAB1
        CONNECT BY LEVEL <= LENGTH(REGEXP_REPLACE(C2, '[^,]+')) + 1) t1,
       (SELECT DISTINCT C1, REGEXP_SUBSTR(C2, '[^,]+', 1, LEVEL) C2
          FROM TAB1
        CONNECT BY LEVEL <= LENGTH(REGEXP_REPLACE(C2, '[^,]+')) + 1) t2
 where t1.c1 = t2.c1
   and t1.c2 <> t2.c2
 group by t1.c1, t1.c2, t2.c2
 order by t1.c1
shxng 2014-04-23
  • 打赏
  • 举报
回复
C2是不定个数的
流浪川 2014-04-23
  • 打赏
  • 举报
回复
引用 7 楼 shxng 的回复:
或者把列C2每行的内容按组合[C(L-2)的组合]拆成多行,然后用WMSYS.WM_CONCAT()就能得到我想要的结果
C2是不定个数还是最多三个。。
shxng 2014-04-23
  • 打赏
  • 举报
回复
oracle有没有组合函数?
shxng 2014-04-23
  • 打赏
  • 举报
回复
只有一个Tab1的表
shxng 2014-04-23
  • 打赏
  • 举报
回复
或者把列C2每行的内容按组合[C(L-2)的组合]拆成多行,然后用WMSYS.WM_CONCAT()就能得到我想要的结果
加载更多回复(6)

17,082

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧