我有以下数据和一个大问题: 在oracle数据库中 我有这样的表中的一些数据..
id version article
1 A B001
2 B B001
3 C B001
4 AA B001
5 AB B001
6 Z B001
7 D B001
我想从这张表中获得最大版本..
Select MAX(version) from articles Where article = 'B001'
它给出了' Z'结果是错误的
但我想' AB'
答案 0 :(得分:2)
with tab(id, version, article) as ( select 1 ,'A', 'B001' from dual union all
select 2, 'B', 'B001' from dual union all
select 3, 'C', 'B001' from dual union all
select 4, 'AA', 'B001' from dual union all
select 5, 'AB', 'B001' from dual union all
select 6, 'Z', 'B001' from dual union all
select 7, 'D', 'B001' from dual)
select trim(max(lpad(version,3,' '))) ver from tab where article = 'B001'
group by article;
答案 1 :(得分:0)
您可以通过两个步骤来完成,1)比较长度和2)选择它们的最大值:
SELECT MAX(t.version) FROM articles t
WHERE article = 'B0001'
AND length(t.version) = (SELECT max(length(s.version)) FROM articles s
WHERE s.article = 'B0001')
如果您希望所有文章都是动态的:
SELECT t.article, MAX(t.version) FROM articles t
WHERE
length(t.version) = (SELECT max(length(s.version)) FROM articles s
WHERE s.article = t.article)
GROUP BY t.article