在Oracle中,仅使用ORDER BY
不会对版本号进行排序。
我的Version_Number
字段被声明为VARCHAR
,我无法更改它。
例如:以下版本:
1.20
1.9
1.18
1.13
1.5
1.11
2.0
1.8
1.3
1.2
1.1
1.0
1.10
应按
排序2.0
1.20
1.18
1.13
1.11
1.10
1.9
1.8
1.5
1.3
1.2
1.1
1.0
我已经研究了几个帖子但是它们似乎都没有真正服务于我的目的,或者答案是针对SQL Server等而不是Oracle。我遇到了这个看起来很有用的特殊sql。
select version_number from mytable
order by lpad(version_number, 4) desc;
以这种方式对版本进行排序:
1.20
1.18
1.13
1.11
1.10
2.0
1.9
1.8
1.5
1.3
1.2
1.1
1.0
我相信这个sql语句适用于SQL Server
:
select version_number from mytable
order by cast ('/' + replace(version_number , '.', '/') + '/' as hierarchyid) desc;
但是,这不适用于Oracle。 Oracle中是否有hierarchyid的替代方案?
是否有人能够提出SQL来准确地对这些版本进行排序?
我已经看到了下面提到的帖子(链接附件)。所以请不要告诉我这篇文章是重复的。
SQL sort by version "number", a string of varying length
How Can I Sort A 'Version Number' Column Generically Using a SQL Server Query
Version number sorting in Sql Server
mysql sorting of version numbers
还有更多。
答案 0 :(得分:4)
这是一种方法。首先按.
之前的数字排序,然后按.
select version_number
from mytable
order by substr(version_number, 1, instr(version_number,'.')-1) desc
,length(substr(version_number, instr(version_number,'.')+1)) desc
,substr(version_number, instr(version_number,'.')+1) desc
答案 1 :(得分:1)
此SQL支持您的输入数据以及任何包含的修订版或构建数字。
with
inputs as (select '1.20' as version_number from dual union all
select '1.9' as version_number from dual union all
select '1.18' as version_number from dual union all
select '1.13' as version_number from dual union all
select '1.5' as version_number from dual union all
select '1.11' as version_number from dual union all
select '2.0' as version_number from dual union all
select '1.8' as version_number from dual union all
select '1.3' as version_number from dual union all
select '1.2' as version_number from dual union all
select '1.1' as version_number from dual union all
select '1.0' as version_number from dual union all
select '1.10' as version_number from dual union all
select ' 3.1 ' as version_number from dual union all
select '3.1.1000' as version_number from dual union all
select '3.1.1' as version_number from dual union all
select '3.1.100' as version_number from dual union all
select '3.1.2.1000' as version_number from dual union all
select '3.1.2.1' as version_number from dual union all
select '3.1.2.100 ' as version_number from dual)
,versions as (select trim(version_number) as version_number,
nvl(LPAD(trim(regexp_substr(version_number, '[^.]+', 1, 1)),5,'0'),'00000') AS Major,
nvl(LPAD(trim(regexp_substr(version_number, '[^.]+', 1, 2)),5,'0'),'00000') AS Minor,
nvl(LPAD(trim(regexp_substr(version_number, '[^.]+', 1, 3)),5,'0'),'00000') AS Revision,
nvl(LPAD(trim(regexp_substr(version_number, '[^.]+', 1, 4)),5,'0'),'00000') AS Build
from inputs
ORDER BY Major desc, Minor desc, Revision desc, Build desc)
--select * from versions;
select version_number from versions;
删除 - 以查看中间结果。
对于OP,将“input as(select ... from dual)”替换为:
inputs as (select version_number from mytable)
答案 2 :(得分:0)
正如Joel Coehoorn建议here,"重构版本号存储,以便每个部分都有自己的列:MajorVersion,MinorVersion,Revision,Build"。
我重新发帖,因为我发现非常非常有帮助!
要扩展,我希望获得MAX
版本号,最后使用此脚本以及Joel的建议。
-- GET MAX VERSION NUMBER
SELECT
REPLACE(vnum, ' ', '') AS versionum
FROM
(SELECT
MAX(LPAD(major, 4) || '.' || LPAD(minor, 4) || '.' || LPAD(revision, 4)) AS vnum
FROM
my_table
ORDER BY
major
, minor
, revision
) tbl1