Find Top Items By Total Score

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bab5470
    New Member
    • May 2010
    • 1

    Find Top Items By Total Score

    I have a SQL table called KbRank with the following columns:

    KBID RANKINGSCORE
    1 100
    2 50
    1 100
    1 75
    3 0
    5 25
    2 100
    3 100
    1 0

    I am attempting to write a SQL query that will find the top 3 KBID's with the highest cummulative ranking score. So I would want to add all of the RANKINGSCORE's for KBID1 up, all of the RANKINGSCORE's for KBID2 up etc, and then sort the results and grab the top 3.

    I'm not much of a SQL guru but I think I would need to use a select statement with SUM to accomplish this. Can anyone point me in the right direction or offer any pointers?

    Thanks
    Brad
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.


    The first example shows you how to get the sum of a group.
    From there, sort on the sum,
    and then select the first few rows using TOP eg: http://vorg.ca/626-the-MS-SQL-equiva...-limit-command

    Comment

    • pantan
      New Member
      • May 2012
      • 1

      #3
      You'll need to use the SUM function and group by to calculate the total for each kbid, and use order by to list the kbid's in descending order. Finally, you'll leverage the subqueryconstru ct to find the top 3 kbid's.

      The SQL to use (this works in MySQL) is:

      select kbid from
      (select kbid, sum(rankingscor e)
      from kbrank
      group by kbid
      order by sum(rankingscor e) desc) li1
      limit 3;
      Last edited by Niheel; May 27 '12, 10:26 PM.

      Comment

      Working...