有表如下
id number codes
--------------------------
1 10 1001011
2 15 0111010
3 16 0110011
4 24 0111101
5 32 1011110
6 12 1000001求codes列的
第一个字符为1 的 所有列number和并上
第二个字符为1 的 所有列number和并上
第三个字符为1 的 所有列number和并上
第四个字符为1 的 所有列number和并上
第五个字符为1 的 所有列number和并上
第六个字符为1 的 所有列number和并上
第七个字符为1 的 所有列number和并上如 :
第一个字符为1 的 所有列number和 为 10+32+12=54
第二个字符为1 的 所有列number和 为 15+16+24=55
.......
第七个字符为1 的 所有列number和 为 10+16+24+12=62希望语句执行结果 如下:
1 54
2 55
.......
7 62
id number codes
--------------------------
1 10 1001011
2 15 0111010
3 16 0110011
4 24 0111101
5 32 1011110
6 12 1000001求codes列的
第一个字符为1 的 所有列number和并上
第二个字符为1 的 所有列number和并上
第三个字符为1 的 所有列number和并上
第四个字符为1 的 所有列number和并上
第五个字符为1 的 所有列number和并上
第六个字符为1 的 所有列number和并上
第七个字符为1 的 所有列number和并上如 :
第一个字符为1 的 所有列number和 为 10+32+12=54
第二个字符为1 的 所有列number和 为 15+16+24=55
.......
第七个字符为1 的 所有列number和 为 10+16+24+12=62希望语句执行结果 如下:
1 54
2 55
.......
7 62
解决方案 »
- 错误提示:FROM keyword not found where expected不知问题何在
- 怎样学好oracle?
- 关于oracle的连接问题
- 在线求解决方法?关于触发器的,对高手是非常简单的问题
- 关于数据文件undotbs01.dbf
- 整整折麽我一天的一个小问题是ASP+ORACLE9I的数据库联接问题
- 帮我解决oracle9i安装问题,送高分
- 在VB.Net 中如何用oo4o获取自定义对象类型中的日期类型
- ┣┫(100分求救,绝不食言)如何动态的表及表字段,再在出的表中加一个trigger?
- 9i的数据能否导入到8i,能实现吗?
- 求一查询语句
- ORACLE触发器(语句级)+TO_CHAR转换时间问题
select 1, sum(num) from test where substr(codes,1,1)=1
union all
select 2,sum(num) from test where substr(codes,2,1)=1
union all
select 3,sum(num) from test where substr(codes,3,1)=1
union all
select 4,sum(num) from test where substr(codes,4,1)=1
union all
select 5,sum(num) from test where substr(codes,5,1)=1
union all
select 6,sum(num) from test where substr(codes,6,1)=1
union all
select 7,sum(num) from test where substr(codes,7,1)=1
with tmp as
(
select 1 id, 10 n, '1001011' code from dual union all
select 2 id, 15 n, '0111010' code from dual union all
select 3 id, 16 n, '0110011' code from dual union all
select 4 id, 24 n, '0111101' code from dual union all
select 5 id, 32 n, '1011110' code from dual union all
select 6 id, 12 n, '1000001' code from dual
)
select l, sum(n) from (
select distinct l, n, code from (
select level l, id, n, code
from tmp
where substr(code,level,1)=1
connect by level <= (select max(length(code)) from tmp)
)
)
group by l
order by l; L SUM(N)
---------- ----------
1 54
2 55
3 87
4 81
5 56
6 73
7 62性能有待改進
查询中雇员的名字第二个字符是M的雇员信息
SELECT empno,ename,comm,salFROM empWHERE ename LIKE '_M%';
说明: _ 匹配一个字符,% 匹配0个或多个字符希望这个对lz有所帮助!
至于实现,楼上都有了。
(
select 1 id, 10 n, '1001011' code from dual union all
select 2 id, 15 n, '0111010' code from dual union all
select 3 id, 16 n, '0110011' code from dual union all
select 4 id, 24 n, '0111101' code from dual union all
select 5 id, 32 n, '1011110' code from dual union all
select 6 id, 12 n, '1000001' code from dual
)
SELECT lev,Sum(n) FROM(
SELECT distinct LEVEL lev,n
FROM tmp
WHERE Decode(SubStr(code,LEVEL,1),'1',1,0)=1
CONNECT BY LEVEL<=Length(code)
)
GROUP BY lev结果:
LEV SUM(N)
----------- ----------
1 54
2 55
3 87
4 81
5 56
6 73
7 62
ID NUM CODES
---------- ---------- --------------------------------------------------------------------------------
1 10 1001011
2 15 0111010
3 16 0110011
4 24 0111101
5 32 1011110
6 12 1000001
6 rows selected
SQL>
SQL> select b.rn, sum(decode(substr(a.codes, b.rn, 1), '1', a.num, 0))
2 from ttt a,
3 (select rownum rn
4 from dual
5 connect by rownum <= (select max(length(codes)) - 1 from ttt)) b
6 group by b.rn;
RN SUM(DECODE(SUBSTR(A.CODES,B.RN
---------- ------------------------------
1 54
2 55
3 87
4 81
5 56
6 73
7 62
7 rows selected
SQL>