Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.server -> Re: SQL Puzzle - Finding Max Sequence

Re: SQL Puzzle - Finding Max Sequence

From: Christopher Beck <clbeck_at_us.oracle.com>
Date: Tue, 12 Oct 1999 18:01:17 -0400
Message-ID: <Nq8DOMwJTg8yMEXgWrIPbJtiqkIQ@4ax.com>


On Tue, 12 Oct 1999 19:55:10 GMT, papollo_bates_at_my-deja.com wrote:

>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.
>

Something like this might work.

clbeck_at_8i> desc foo

 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 ID                                                 NUMBER
 SEQNO                                              NUMBER
 GRADE                                              VARCHAR2(1)


clbeck_at_8i> select * from foo;

        ID SEQNO G
---------- ---------- -

       101          1 A
       101          2 B
       101          3 A
       102          1 C
       103          1 D
       103          2 B
       103          3 F

7 rows selected.

clbeck_at_8i> select id, seqno, grade
  2 from foo f1
  3 where seqno >= ( select max(seqno)

  4                      from foo f2
  5                     where f1.id = f2.id
  6                     group by id );

        ID      SEQNO G
---------- ---------- -
       101          3 A
       102          1 C
       103          3 F


>Thanks!
>
>
>Sent via Deja.com http://www.deja.com/
>Before you buy.

--
Christopher Beck
Oracle Corporation
clbeck_at_us.oracle.com
Reston, VA.



Opinions are mine and do not necessarily reflect those of Oracle Corporation Received on Tue Oct 12 1999 - 17:01:17 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US