我在数据库中有以下数据,主键是字段SEQ
,我希望选择具有最大seq
的数据:
ID SEQ FILE
1007 1 abc
1007 2 def
以下查询无效,但我希望做同样的事情。
SELECT * FROM table1 WHERE id = '1007' AND Max(seq)
答案 0 :(得分:3)
SELECT id, seq, file
FROM (
select id, seq, file,
max(seq) over (partition by id) as max_seq
from table1
WHERE id = '1007'
) t
where seq = max_seq;
答案 1 :(得分:0)
如果我理解正确,你希望这样的事情
select *
from (select t.*,
max(t.seq)
keep (dense_rank first order by t.seq desc)
over (partition by t.id) max#
from table1 t)
where seq = max#
答案 2 :(得分:0)
另一种方法:
select id,seq,"FILE" from
(select t1.*,row_number() over (partition by id order by seq desc) cont_seq
from your_table t1)
where cont_seq = 1
order by seq;
这将为您提供按ID分组且具有最大seq值的所有行。如果你想要一个特定的值,只需在where子句中添加条件,如下所示:
select id,seq,"FILE" from
(select t1.*,row_number() over (partition by id order by seq desc) cont_seq
from your_table t1)
where cont_seq = 1 and id = '1007'
order by seq;
答案 3 :(得分:0)
select * from alber.table1;
MYID SEQ FILENAME
1007 2 abc
1007 10 def
1008 45 abc
1008 9 def
SELECT myid, seq, filename from alber.table1 mq
where seq = (select max(seq) from alber.table1 sq where sq.myid = mq.myid);
MYID SEQ FILENAME
1007 10 def
1008 45 abc