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

isnumeric関数の場所(@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 に答える