-1

我有一个表说变量,其中存在一个名为“name”的列。该字段的值如下 -

    abc_R01
    abc_R02
    abc_R03
    pqrs_R01
    pqrs_R02
    xyz_R01
    xyz_R02
    xyz_R03
    xyz_R04

现在我的选择标准就像我必须选择每个变量的最新变量名称,即我可以触发单个/复杂查询以获得结果,例如 -

    abc_R03
    pqrs_R02
    xyz_R04

或者我需要通过我的代码处理它,这里 _Rxx 表示我的变量的修订。请帮我。

4

3 回答 3

1

这也能帮助你,

WITH t(NAME) AS
(
SELECT 'abc_R01' FROM dual
UNION
SELECT 'abc_R02' FROM dual
UNION
SELECT 'abc_R03' FROM dual
UNION
SELECT 'pqrs_R01' FROM dual
UNION
SELECT 'pqrs_R02' FROM dual
UNION
SELECT 'xyz_R01' FROM dual
UNION
SELECT 'xyz_R02' FROM dual
UNION
SELECT 'xyz_R03' FROM dual
UNION
SELECT 'xyz_R04' FROM dual
)
SELECT NAME
FROM (
     SELECT substr(NAME, 0, instr(NAME, 'R')),
            MAX(NAME) NAME
     FROM  t
     GROUP BY substr(NAME, 0, instr(NAME, 'R'))
     );
于 2013-10-10T06:25:50.827 回答
1

请试试:

select 
  Col
from
(
  select
    Col,
    row_number() over (partition BY substr(Col, 0, instr(Col, '_')) order by regexp_substr(col, '\d+$') DESC) rn
  from YourTable
)x where rn=1;

在 10 G 中检查以下查询

select 
  Col
from
(
  select
    Col,
    row_number() over (partition BY substr(Col, 0, instr(Col, '_')) order by substr(col, instr(col, 'R')+1) DESC) rn
  from YourTable
)x where rn=1;

SQL 小提琴演示

于 2013-10-10T04:43:43.927 回答
1
select name
from
(
select 
  substr("name",0,length("name")-1) as n1,max("name") as name
from Table1
   group by substr("name",0,length("name")-1)
)t

SQL小提琴

于 2013-10-10T04:33:42.757 回答