我有一個數據:如何根據條件對數據進行分組並依次對這些組進行編號?
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;
對不起!我不明白你的問題。你能舉一個你預期的結果的例子嗎? – Tim 2012-08-07 07:59:27
我已將它添加到我的問題中 – DmitryB 2012-08-07 08:13:06
@DmitryB:您需要確切的順序,或者您只需要說出哪個字段只是數字,哪個字段包含字母? – mcha 2012-08-07 08:25:50