Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.server -> Re: SQL Puzzle - Finding Max Sequence
Given:
ID Seqnence_No Grade 100 1 A 101 1 C 102 1 A 102 2 B 102 3 C 103 1 C 103 2 B 103 3 A
then:
select id, grade
from my_table
where (id, seq) in
(select id, max(seq)
from t2
group by id)
and grade in ('A', 'B')
/
produces:
ID GRADE
--------- -----
100 A 103 A
HTH, Yosi
fumi wrote:
> <papollo_bates_at_my-deja.com> wrote in message
> news:7u03ml$a4m$1_at_nnrp1.deja.com...
> > I have a table with three fields:
> >
> > 1) ID is a number field
> > 2) Sequence_No is a number field
> > 3) Grade is a VARCHAR2(1), holding A, B, C, D, E
> >
> > There are 100 individual IDs, each with more than one record. As a
> > record is inserted for an ID, the next Sequence_No for that ID is used.
> > So, ID 100 has Sequence_No values of 1, 2, 3, 4, etc., as does ID 101
> > and every other ID
> >
> > I want to select all IDs with a Grade of A or B, but I can only look at
> > their highest Sequence_No when checking their grade. I can"t figure out
> > the MAX(Sequence_No) part.
>
>
> ID Seqnence_No Grade
> 100 1 A
> 101 1 C
> 102 1 A
> 102 2 B
> 102 3 C
> 103 1 C
> 103 2 B
> 103 3 A
>
>