Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2004
    Location
    Far away from heaven
    Posts
    194

    Unanswered: top 10 charges for each itemid (was "Need Simple Solution")

    I've a table with two columns ITEMID and CHARGES.
    Multiple ITEMIDs with different CHARGES exists. I want to query TOP 10 CHARGES for each ITEMID in the table; how come i achieve this without typing the TOP 10 for each ITEMID in the table. The table has two many itemids and i want to view 10 for each with maximum charges.


    Howdy!

  2. #2
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    Here is a general solution to your problem:

    Code:
    declare @N int
    set     @N = 5  --The number of records to return for each grouping.
    select  YourTable.YourColumns
    from    YourTable
            inner join YourTable ThetaTable
            on YourTable.GroupColumns = ThetaTable.GroupColumns
            and YourTable.SortColumn <= ThetaTable.SortColumn
    group by YourTable.YourColumns
    having count(*) <= @N
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

Posting Permissions

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