DBAsupport.com Forums - Powered by vBulletin
Results 1 to 4 of 4

Thread: Query

  1. #1
    Join Date
    Mar 2002
    Posts
    301
    My Tables has values like this.

    CateID Name SubID
    1 123 1
    1 234 2
    1 435 3
    1 435 4
    1 435 5
    2 123 1
    2 123 2
    2 123 3
    2 123 4
    2 123 5
    3 123 1
    3 123 2
    3 123 3
    3 123 4

    I want a query such that i want to select only 2 rows in each catid.
    ie)My O/P should be expected like this.
    CateID Name SubID
    1 123 1
    1 234 2
    2 123 1
    2 123 2
    3 123 1
    3 123 2

    Is it possible by a Query.
    Say No To Plastics

  2. #2
    Join Date
    Dec 2001
    Location
    UK
    Posts
    1,684
    SELECT CateID, Name, SubID
    FROM my_table
    WHERE SubID <= 2
    ORDER BY CateID, Name, SubID;

    Is this what you mean?

    Cheers
    Tim...
    OCP DBA 7.3, 8, 8i, 9i, 10g, 11g
    OCA PL/SQL Developer
    Oracle ACE Director
    My website: oracle-base.com
    My blog: oracle-base.com/blog

  3. #3
    Join Date
    Aug 2000
    Location
    Belgium
    Posts
    342
    assume the name of your table is GDN.


    SQL> select * from gdn;

    CATID NAME SUBID
    ---------- -------------------- ----------
    1 123 1
    1 234 2
    1 435 3
    1 435 4
    1 435 5
    2 123 1
    2 123 2
    2 123 3
    2 123 4
    2 123 5
    3 123 1

    CATID NAME SUBID
    ---------- -------------------- ----------
    3 123 2
    3 123 3
    3 123 4

    14 rows selected.

    SQL> get xx
    1 select * from (
    2 select catid,name,subid,rank() over ( partition by catid order by name,subid ) r
    3 from gdn
    4 ) tab
    5* where tab.r < 3
    SQL> /

    CATID NAME SUBID R
    ---------- -------------------- ---------- ----------
    1 123 1 1
    1 234 2 2
    2 123 1 1
    2 123 2 2
    3 123 1 1
    3 123 2 2

    6 rows selected.


    Hope this helps
    Gert

  4. #4
    Join Date
    Mar 2002
    Posts
    301
    Thanks mate.
    Say No To Plastics

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Click Here to Expand Forum to Full Width