Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8631343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:12:23+00:00 2026-06-12T09:12:23+00:00

I have a problem using RANK() in SQL Server. Here’s my code: SELECT contendernum,

  • 0

I have a problem using RANK() in SQL Server.

Here’s my code:

SELECT contendernum,
       totals, 
       RANK() OVER (PARTITION BY ContenderNum ORDER BY totals ASC) AS xRank
FROM (
   SELECT ContenderNum,
          SUM(Criteria1+Criteria2+Criteria3+Criteria4) AS totals
   FROM Cat1GroupImpersonation
   GROUP BY ContenderNum
) AS a

The results for that query are:

contendernum    totals    xRank
          1       196        1
          2       181        1
          3       192        1
          4       181        1
          5       179        1

What my desired result is:

contendernum    totals    xRank
          1       196        1
          2       181        3
          3       192        2
          4       181        3
          5       179        4

I want to rank the result based on totals. If there are same value like 181, then two numbers will have the same xRank.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:12:23+00:00Added an answer on June 12, 2026 at 9:12 am

    Change:

    RANK() OVER (PARTITION BY ContenderNum ORDER BY totals ASC) AS xRank
    

    to:

    RANK() OVER (ORDER BY totals DESC) AS xRank
    

    Have a look at this example:

    SQL Fiddle DEMO

    You might also want to have a look at the difference between RANK (Transact-SQL) and DENSE_RANK (Transact-SQL):

    RANK (Transact-SQL)

    If two or more rows tie for a rank, each tied rows receives the same
    rank. For example, if the two top salespeople have the same SalesYTD
    value, they are both ranked one. The salesperson with the next highest
    SalesYTD is ranked number three, because there are two rows that are
    ranked higher. Therefore, the RANK function does not always return
    consecutive integers.

    DENSE_RANK (Transact-SQL)

    Returns the rank of rows within the partition of a result set, without
    any gaps in the ranking. The rank of a row is one plus the number of
    distinct ranks that come before the row in question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem using a local SQL Server CE database with C# and
I have a problem using JSON and arrays. Here is my code: while($row =
i have problem using LIKE structure in DB2 : for example: select * from
i have a problem using the Catch Clipboard Events code found on this link
I'm having some problem in using order by in mysql. I have a table
I have built a report using Report Builder 3.0 (that uses SQL Server 2008
Here is my problem. I have a select statement something like this: select *
I have a problem using a SeekBar in my code... I'd like to have
I have problem using arabic font for iOS. All fonts have the same render,
I have problem while using jquery maskedinput with asp.net textbox. I have a check

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.