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

Thread: query help

  1. #1
    Join Date
    Mar 2001
    Location
    south africa
    Posts
    401

    query help

    hello

    we have order types and orders tables and for every ordertype we have many orders

    i am looking for a query to dispay 5 orders for every ordertype

    how can we get this

    thanks

  2. #2
    Join Date
    May 2000
    Location
    ATLANTA, GA, USA
    Posts
    3,135
    Assume both tables have type column.

    select ...
    from ordertype a, orders b
    where a.type = b.type
    and rownum <= 5;

  3. #3
    Join Date
    Mar 2007
    Location
    Ft. Lauderdale, FL
    Posts
    3,555
    prodadmin said "dispay 5 orders for every ordertype"
    ...
    Quote Originally Posted by tamilselvan
    select ...
    from ordertype a, orders b
    where a.type = b.type
    and rownum <= 5;
    I'm wondering how this query solves that business requirement?
    Pablo (Paul) Berzukov

    Author of Understanding Database Administration available at amazon and other bookstores.

    Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.

  4. #4
    Join Date
    Mar 2001
    Location
    south africa
    Posts
    401
    hello tamilselvan , query you provided results last 5 rows

    i am looking for some thing like this

    ordertype -orderid
    1-12
    1-13
    1-14
    1-15
    1-16
    2-34
    2-45
    2-67
    2-78
    3-99
    3-98
    3-97



    thank you

  5. #5
    Join Date
    Mar 2007
    Location
    Ft. Lauderdale, FL
    Posts
    3,555
    My preferred solutions will involve two cursors.

    Outer cursor for order_type and inner cursor for the five orders you want to show for each order_type.
    Pablo (Paul) Berzukov

    Author of Understanding Database Administration available at amazon and other bookstores.

    Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.

  6. #6
    Join Date
    May 2000
    Location
    ATLANTA, GA, USA
    Posts
    3,135
    Sorry, Analytic function will solve the problem
    Code:
          select type, order_id, 
                   row_number() 
                   over (partition by type order by order_id) RN  
          from   (  select a.type , b.order_id
                        from ordertype a, orders b
                      where a.type = b.type)
         where RN <= 5;

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