我有一张表,上面有这样的数据:
Id | Version | isLive
-------+---------+-------
comp1 | 2 | true
comp1 | 3 | true
comp1 | 4 | false
comp1 | 1 | true
comp2 | 4 | true
comp2 | 1 | false
我想获取每个ID具有最高版本号的行。因此,每个ID只能有一行。
select id
from mytable
group by id
这将随机返回数据。
答案 0 :(得分:0)
来自here
SELECT * FROM (SELECT * FROM MyTbl ORDER BY version ) WHERE rownum = 1;
答案 1 :(得分:0)
这是您想要的吗?
select id,max(distinct version) from
mytable
group by id order by id
答案 2 :(得分:0)
如何?
SQL> with test (id, version, islive) as
2 (select 'comp1', 2, 'true' from dual union all
3 select 'comp1', 3, 'true' from dual union all
4 select 'comp1', 4, 'false' from dual union all
5 select 'comp1', 1, 'true' from dual union all
6 select 'comp2', 4, 'true' from dual union all
7 select 'comp2', 1, 'false' from dual
8 )
9 select id, version, islive
10 from (select id, version, islive,
11 row_number() over (partition by id order by version desc) rn
12 from test
13 )
14 where rn = 1;
ID VERSION ISLIV
----- ---------- -----
comp1 4 false
comp2 4 true
SQL>