我有下一个数据:
with t as
(select 1 as id, '1324345' as amount, 7821 as code
from dual
union all
select 2 as id, 'current' as amount, 2210 as code
from dual
union all
select 3 as id, 'link' as amount, 2210 as code
from dual
union all
select 4 as id, '56236400' as amount, 6740 as code
from dual
union all
select 5 as id, '45562330' as amount, 5578 as code
from dual
union all
select 6 as id, '34875930' as amount, 5828 as code
from dual
union all
select 7 as id, 'current' as amount, 8520 as code
from dual
union all
select 8 as id, 'link' as amount, 8520 as code
from dual
union all
select 9 as id, '6731347060' as amount, 4740 as code
from dual
union all
select 10 as id, '346008600' as amount, 6575 as code
from dual)
select * from t
我想得到以下信息:
with t as
(select 1 as id, '1324345' as amount, 7821 as code, 1 as group_id
from dual
union all
select 2 as id, 'current' as amount, 2210 as code, 2 as group_id
from dual
union all
select 3 as id, 'link' as amount, 2210 as code, 2 as group_id
from dual
union all
select 4 as id, '56236400' as amount, 6740 as code, 3 as group_id
from dual
union all
select 5 as id, '45562330' as amount, 5578 as code, 3 as group_id
from dual
union all
select 6 as id, '34875930' as amount, 5828 as code, 3 as group_id
from dual
union all
select 7 as id, 'current' as amount, 8520 as code, 4 as group_id
from dual
union all
select 8 as id, 'link' as amount, 8520 as code, 4 as group_id
from dual
union all
select 9 as id, '6731347060' as amount, 4740 as code, 5 as group_id
from dual
union all
select 10 as id, '346008600' as amount, 6575 as code, 5 as group_id
from dual)
select * from t
条件是“金额”字段的值。它可以是数字或文本。
UPD:预期结果:
id | amount | code | group_id
---------------------------------------------
1 | 1324345 | 7821 | 1
---------------------------------------------
2 | current | 2210 | 2
---------------------------------------------
3 | link | 2210 | 2
---------------------------------------------
4 | 56236400 | 6740 | 3
---------------------------------------------
5 | 45562330 | 5578 | 3
---------------------------------------------
6 | 34875930 | 5828 | 3
---------------------------------------------
7 | current | 8520 | 4
---------------------------------------------
8 | link | 8520 | 4
---------------------------------------------
9 | 6731347060 | 4740 | 5
---------------------------------------------
10 | 346008600 | 6575 | 5
---------------------------------------------
编辑:最佳解决方案:
with tmain as
(select t.*,
decode(isnumeric(Amount),
lag(isnumeric(Amount)) over(order by id),
null,
1) lg
from t
order by id)
select id, amount, code, count(lg) over(order by id) group_id from tmain
isumeric函数在哪里(基于@valexhome 答案):
CREATE OR REPLACE FUNCTION ISNUMERIC (Str IN CHAR) RETURN NUMBER AS
TMP int;
BEGIN
if Str is null then
return(null);
end if;
TMP:=TO_NUMBER(Str);
RETURN (1);
EXCEPTION
WHEN OTHERS THEN
RETURN (0);
END;