日期:2014-05-17  浏览次数:20670 次

求一sql语句。有一个Oracle数据表,内容如下:
id       IMAGEID
1 5,24
2 192
3
4
5
6 5,24,121,3,34
7
8
9
10
11


其中IMAGEID没写的表明该值为空。

我想统计IMAGEID中值的个数,有逗号要拆开统计。比如上面的结果应该是5、24、192、5、24、121、3、34个数的总合。(应该是8)
这样的sql语句该怎样写呀?

当然表中的记录不只是11条,会有很多,不可能一个一个地数呀。

------解决方案--------------------
--原来这样可以 :)

--测试数据
create table ttt( id int, IMAGEID varchar2(1000));
insert into ttt values(1, '5,24 ');
insert into ttt values(2, '192 ');
insert into ttt(id) values(3);
insert into ttt(id) values(4);
insert into ttt values(5, '5,24,121,3,34 ');
--执行查询
select sum(length(IMAGEID)-length(replace(IMAGEID, ', ', ' ')) +1) sum
from ttt
where imageid is not null

--输出结果
8
------解决方案--------------------
--这回可以了吧

create or replace procedure sp_GetCount( acount out int )
is
begin
select sum(length(IMAGEID)-length(replace(IMAGEID, ', ', ' ')) +1) into acount
from ttt
where imageid is not null
end sp_GetCount;