1

我有下一个数据:

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;
4

2 回答 2

1

这是在查询运行之前定义的函数 ISNUMERIC:

CREATE OR REPLACE FUNCTION ISNUMERIC (Str IN CHAR) RETURN NUMBER AS
    TMP int;
BEGIN
    if Str is null then
       return(null);
    end if;
    --if input null return NULL
    TMP:=TO_NUMBER(Str);
    RETURN (1);
EXCEPTION
    WHEN OTHERS THEN
       RETURN (0);
END;

这是查询:

select id, amount, code, 
(
select count(id) 
from t Tab 
where tab.id<=t.id
  and 
  isnumeric(Amount)<>nvl(isnumeric((select Amount from t d1 where d1.id=(select max(d.id) from t d where (d.id<Tab.id)))),isnumeric(Amount)-1)
) Group_id

from t order by id
于 2012-08-08T06:38:13.840 回答
1

我找到了另一个解决方案:

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

它适用于大量数据。

于 2012-08-15T10:55:36.407 回答