楼主: newkid

连TOM也答不上来的SQL问题,你来试试看!

[复制链接]
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
11#
发表于 2010-4-9 11:11 | 只看该作者

回复 #10 dingjun123 的帖子

你的更加通用,我的超过38个逗号就不行了
SQL> select bitand(1e39,1e35) from dual;
select bitand(1e39,1e35) from dual
       *
第 1 行出现错误:
ORA-01426: 数字溢出

使用道具 举报

回复
论坛徽章:
26
2010年世界杯参赛球队:智利
日期:2010-07-03 17:16:26比亚迪
日期:2014-01-16 17:12:41宝马
日期:2014-01-24 10:32:252014年新春福章
日期:2014-02-18 16:44:08马上有对象
日期:2014-02-18 16:44:08马上有对象
日期:2014-03-05 21:30:32马上有车
日期:2014-03-11 16:46:45优秀写手
日期:2014-03-25 05:59:50马上加薪
日期:2014-03-26 16:46:30问答徽章
日期:2014-05-09 16:40:36
12#
发表于 2010-4-9 11:57 | 只看该作者
sql真强大啊,特别是oracle的sql

使用道具 举报

回复
论坛徽章:
1088
金色在线徽章
日期:2007-04-25 04:02:08金色在线徽章
日期:2007-06-29 04:02:43金色在线徽章
日期:2007-03-11 04:02:02在线时间
日期:2007-04-11 04:01:02在线时间
日期:2007-04-12 04:01:02在线时间
日期:2007-03-07 04:01:022008版在线时间
日期:2010-05-01 00:01:152008版在线时间
日期:2011-05-01 00:01:342008版在线时间
日期:2008-06-03 11:59:43ITPUB年度最佳技术原创精华奖
日期:2013-03-22 13:18:30
13#
发表于 2010-4-9 12:27 | 只看该作者
我那个效率不好,有笛卡尔积的,所以有distinct,数量多了很慢

使用道具 举报

回复
论坛徽章:
40
授权会员
日期:2009-03-04 17:06:25最佳人气徽章
日期:2013-03-19 17:24:25SQL极客
日期:2013-12-09 14:13:35优秀写手
日期:2013-12-18 09:29:09ITPUB元老
日期:2015-03-04 13:33:34白羊座
日期:2016-03-11 13:49:34乌索普
日期:2017-11-17 11:40:00
14#
发表于 2010-4-9 12:54 | 只看该作者

回复 #13 dingjun123 的帖子

with tmp as
    (select  TRIM(REGEXP_SUBSTR('a,b,c,d,e', '[^,]+', 1, level)) val from dual
       connect by level <= length(regexp_replace('a,b,c,d,e','[^,]*'))+1)
select replace(col1,',') col1,
           replace(translate('a,b,c,d,e',col1,' '),' ') col2
   from (select sys_connect_by_path (val, ',') col1
           from (select val, rownum rn from tmp)
        connect by val > prior val )
   where replace(translate('a,b,c,d,e',col1,' '),' ') is not null
  order by col1;

可以不用表链接吧,直接取rownum就可以
逗号的问题是比较麻烦些,不知道还有什么好的办法 呵呵

使用道具 举报

回复
论坛徽章:
1088
金色在线徽章
日期:2007-04-25 04:02:08金色在线徽章
日期:2007-06-29 04:02:43金色在线徽章
日期:2007-03-11 04:02:02在线时间
日期:2007-04-11 04:01:02在线时间
日期:2007-04-12 04:01:02在线时间
日期:2007-03-07 04:01:022008版在线时间
日期:2010-05-01 00:01:152008版在线时间
日期:2011-05-01 00:01:342008版在线时间
日期:2008-06-03 11:59:43ITPUB年度最佳技术原创精华奖
日期:2013-03-22 13:18:30
15#
发表于 2010-4-9 13:10 | 只看该作者
还可以简化下
with tmp as
    (select  TRIM(REGEXP_SUBSTR('a,b,c,d,e', '[^,]+', 1, level)) val from dual
       connect by level <= length(regexp_replace('a,b,c,d,e','[^,]*'))+1)
select replace(col1,',') col1,
           replace(translate('a,b,c,d,e',col1,' '),' ') col2
   from (select sys_connect_by_path (val, ',') col1
           from tmp
        connect by val > prior val )
    where   replace(translate('a,b,c,d,e',col1,' '),' ') is not null
  order by col1;
  

COL1                                                                         COL2
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
a                                                                               bcdefghijklm
ab                                                                             cdefghijklm
abc                                                                            defghijklm
abcd                                                                          efghijklm
abcde                                                                        fghijklm
abcdef                                                                       ghijklm
abcdefg                                                                     hijklm
abcdefgh                                                                   ijklm
abcdefghi                                                                   jklm
abcdefghij                                                                  klm
abcdefghijk                                                                lm
abcdefghijkl                                                               m
abcdefghijkm                                                             l
-----

使用道具 举报

回复
论坛徽章:
1088
金色在线徽章
日期:2007-04-25 04:02:08金色在线徽章
日期:2007-06-29 04:02:43金色在线徽章
日期:2007-03-11 04:02:02在线时间
日期:2007-04-11 04:01:02在线时间
日期:2007-04-12 04:01:02在线时间
日期:2007-03-07 04:01:022008版在线时间
日期:2010-05-01 00:01:152008版在线时间
日期:2011-05-01 00:01:342008版在线时间
日期:2008-06-03 11:59:43ITPUB年度最佳技术原创精华奖
日期:2013-03-22 13:18:30
16#
发表于 2010-4-9 13:11 | 只看该作者
power(2,n)-2种,这个没有笛卡尔积了,其实能解决逗号问题的,大不了可以写个函数,我想逗号问题肯定还有好办法

使用道具 举报

回复
论坛徽章:
69
生肖徽章2007版:羊
日期:2008-11-14 14:42:19复活蛋
日期:2011-08-06 08:59:05ITPUB十周年纪念徽章
日期:2011-11-01 16:19:412012新春纪念徽章
日期:2012-01-04 11:49:542012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:20版主4段
日期:2012-05-15 15:24:11
17#
发表于 2010-4-9 13:18 | 只看该作者
各位好强大...

使用道具 举报

回复
论坛徽章:
3
奥运会纪念徽章:铁人三项
日期:2008-08-14 11:45:20生肖徽章2007版:马
日期:2009-03-20 11:26:202010新春纪念徽章
日期:2010-03-01 11:19:07
18#
发表于 2010-4-9 13:45 | 只看该作者
最近脑子不好使,凑合看看
SQL> with tmp as (select 'a,b,c' col from dual)
  2  select decode(length(a.col),1,a.col,substr(a.col,0,1)||','||substr(a.col,2,1)) col,
decode(length(b.col),1,b.col,substr(b.col,0,1)||','||substr(b.col,2,1)) col1 from
(select distinct decode(a.col,b.col,a.col,c.col,a.col,a.col||decode(b.col,c.col,b.col,b.col||c.col)) col from
(select substr(col,rownum,1) col,rownum rn from (select replace(col,',') col from tmp) connect by rownum<=length(col)) a,
(select substr(col,rownum,1) col,rownum rn from (select replace(col,',') col from tmp) connect by rownum<=length(col)) b,
(select substr(col,rownum,1) col,rownum rn from (select replace(col,',') col from tmp) connect by rownum<=length(col)) c
where a.rn<=b.rn and a.rn<=c.rn and b.rn<=c.rn) a,
(select distinct decode(a.col,b.col,a.col,c.col,a.col,a.col||decode(b.col,c.col,b.col,b.col||c.col)) col from
(select substr(col,rownum,1) col,rownum rn from (select replace(col,',') col from tmp) connect by rownum<=length(col)) a,
(select substr(col,rownum,1) col,rownum rn from (select replace(col,',') col from tmp) connect by rownum<=length(col)) b,
(select substr(col,rownum,1) col,rownum rn from (select replace(col,',') col from tmp) connect by rownum<=length(col)) c
where a.rn<=b.rn and a.rn<=c.rn and b.rn<=c.rn) b
where a.col not like '%'||b.col||'%' and b.col not like '%'||a.col||'%'
and length(a.col)<>length(b.col) and length(a.col)<>3 and length(b.col)<>3  3    4    5    6    7    8    9   10   11   12   13   14   15  ;

COL    COL1
------ ------
a,b    c
a      b,c
b      a,c
a,c    b
c      a,b
b,c    a

使用道具 举报

回复
论坛徽章:
40
授权会员
日期:2009-03-04 17:06:25最佳人气徽章
日期:2013-03-19 17:24:25SQL极客
日期:2013-12-09 14:13:35优秀写手
日期:2013-12-18 09:29:09ITPUB元老
日期:2015-03-04 13:33:34白羊座
日期:2016-03-11 13:49:34乌索普
日期:2017-11-17 11:40:00
19#
发表于 2010-4-9 13:46 | 只看该作者
with tmp as
    (select  TRIM(REGEXP_SUBSTR('a,b,c,d,e', '[^,]+', 1, level)) val from dual
       connect by level <= length(regexp_replace('a,b,c,d,e','[^,]*'))+1)
select LTRIM(col1,',') col1,
       LTRIM(REGEXP_REPLACE(translate('a,b,c,d,e',col1,','),'(,){2,}', ','),',') col2
  from (select sys_connect_by_path (val, ',') col1
          from tmp
        connect by val > prior val )
where LTRIM(translate('a,b,c,d,e',col1,','),',') is not null
order by col1;

改成正则表达式替换逗号  呵呵

[ 本帖最后由 jiqing1004 于 2010-4-9 13:58 编辑 ]

使用道具 举报

回复
论坛徽章:
5
2010年世界杯参赛球队:意大利
日期:2010-03-23 15:18:272010年世界杯参赛球队:阿尔及利亚
日期:2010-05-23 13:13:032010广州亚运会纪念徽章:乒乓球
日期:2011-04-06 11:31:16
20#
发表于 2010-4-9 13:54 | 只看该作者
SYSTEM@dp > with t as (select rownum rn,trim(regexp_substr('a,b,c,d,e', '[^,]+', 1, level)) x from dual
connect by level <= length(regexp_replace('a,b,c,d,e','[^,]*'))+1)
,m as (select sys_connect_by_path(x,',')||',' p,x,level from t where level<=(select count(rn)/2 from t) connect by prior rn<rn)
,n as (select trim(both ',' from p) j,(select wm_concat(x) from t where instr(m.p,','||x||',')=0) k from m order by p)
select j col1,k col2 from n union select k ,j from n;
  2    3    4    5  
COL1           COL2
---------- ----------
a           b,c,d,e
a,b           c,d,e
a,b,c           d,e
a,b,c,d    e
a,b,c,e    d
a,b,d           c,e
a,b,d,e    c
a,b,e           c,d
a,c           b,d,e
a,c,d           b,e
a,c,d,e    b
a,c,e           b,d
a,d           b,c,e
a,d,e           b,c
a,e           b,c,d
b           a,c,d,e
b,c           a,d,e
b,c,d           a,e
b,c,d,e    a
b,c,e           a,d
b,d           a,c,e
b,d,e           a,c
b,e           a,c,d
c           a,b,d,e
c,d           a,b,e
c,d,e           a,b
c,e           a,b,d
d           a,b,c,e
d,e           a,b,c
e           a,b,c,d

30 rows selected.

SYSTEM@dp > with t as (select rownum rn,trim(regexp_substr('a,b,c,d,e,f', '[^,]+', 1, level)) x from dual
connect by level <= length(regexp_replace('a,b,c,d,e,f','[^,]*'))+1)
,m as (select sys_connect_by_path(x,',')||',' p,x,level from t where level<=(select count(rn)/2 from t) connect by prior rn<rn)
,n as (select trim(both ',' from p) j,(select wm_concat(x) from t where instr(m.p,','||x||',')=0) k from m order by p)
select j col1,k col2 from n union select k,j from n;
  2    3    4    5  
COL1           COL2
---------- ----------
a           b,c,d,e,f
a,b           c,d,e,f
a,b,c           d,e,f
a,b,c,d    e,f
a,b,c,d,e  f
a,b,c,d,f  e
a,b,c,e    d,f
a,b,c,e,f  d
a,b,c,f    d,e
a,b,d           c,e,f
a,b,d,e    c,f
a,b,d,e,f  c
a,b,d,f    c,e
a,b,e           c,d,f
a,b,e,f    c,d
a,b,f           c,d,e
a,c           b,d,e,f
a,c,d           b,e,f
a,c,d,e    b,f
a,c,d,e,f  b
a,c,d,f    b,e
a,c,e           b,d,f
a,c,e,f    b,d
a,c,f           b,d,e
a,d           b,c,e,f
a,d,e           b,c,f
a,d,e,f    b,c
a,d,f           b,c,e
a,e           b,c,d,f
a,e,f           b,c,d
a,f           b,c,d,e
b           a,c,d,e,f
b,c           a,d,e,f
b,c,d           a,e,f
b,c,d,e    a,f
b,c,d,e,f  a
b,c,d,f    a,e
b,c,e           a,d,f
b,c,e,f    a,d
b,c,f           a,d,e
b,d           a,c,e,f
b,d,e           a,c,f
b,d,e,f    a,c
b,d,f           a,c,e
b,e           a,c,d,f
b,e,f           a,c,d
b,f           a,c,d,e
c           a,b,d,e,f
c,d           a,b,e,f
c,d,e           a,b,f
c,d,e,f    a,b
c,d,f           a,b,e
c,e           a,b,d,f
c,e,f           a,b,d
c,f           a,b,d,e
d           a,b,c,e,f
d,e           a,b,c,f
d,e,f           a,b,c
d,f           a,b,c,e
e           a,b,c,d,f
e,f           a,b,c,d
f           a,b,c,d,e

62 rows selected.

[ 本帖最后由 kingtsi 于 2010-4-9 13:56 编辑 ]

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表