我的Oracle数据库中有一个名为ID的列,它是一个字符串,其值为859188_1,859188_2,859188_3,...,859188_n。 ' _'之后的值是批号,即1,2,3,...,n。我需要根据此批号选择最小ID。这里的最小值是859188_1。
答案 0 :(得分:0)
...试
select id
from yourTable
where to_number(regexp_substr(ID, '[^_]+$')) =
(select min(to_number(regexp_substr(t2.ID, '[^_]+$')))
from yourTable t2
)
答案 1 :(得分:0)
您需要根据 _
之后部分选择最小值。
如果 - 在您的示例中 - 前六个字符都相同,您只需执行以下操作:
select min(id) keep (dense_rank first order by length(id) asc, id asc) as min_id
from t;
答案 2 :(得分:0)
也许是这样的......
with
inputs ( id ) as (
select '100_1' from dual union all
select '100_3' from dual union all
select '100_12' from dual union all
select '101_3' from dual union all
select '101_5' from dual union all
select '101_9' from dual
)
-- end of test data; solution (SQL query) begins below this line
select min(id) keep (dense_rank first
order by to_number(substr(id, instr(id, '_') + 1))) as id
from inputs
group by substr(id, 1, instr(id, '_') - 1)
;
ID
-----
100_1
101_3