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

  • Home
  • SEARCH
  • 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 3935500
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:50:13+00:00 2026-05-19T23:50:13+00:00

In SQL Server 2008 R2, I’m trying to work out the rank of an

  • 0

In SQL Server 2008 R2, I’m trying to work out the rank of an individual item, as determined by the number of occurances. I can work out the rank of a number of items, i.e. the top ten instances of a colour, but I can’t get the rank of a specific colour.

I.E I have a table of Vehicles, which has a Vehicles.ColourID column.

For a specific ColourID I want to determine the rank of it.

i.e.

  • ColourID 7 has 10 instances
  • ColourID 10 has 5 instances
  • ColourID 2 has 1 instance

In that result-set, ColourID 7 has a rank of 1, ColourID 10 has a rank of 2 and so on.

I would like to query for ColourID 10 and find it has a rank of 2. This is what I am struggling with.

I’ve tried using derived tables and common table expressions, but in both cases they bring back a rank of 1.

Does anyone know how I could query for the rank of a single item?

  • 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-05-19T23:50:13+00:00Added an answer on May 19, 2026 at 11:50 pm

    To rank all the colours:

    ;with cteCount as (
        select ColourID, count(*) as ColourCount
            from Vehicles
            group by ColourID
    )
    select ColourID, ColourCount, 
           row_number() over (order by ColourCount desc) as Rank
        from cteCount
    

    You can then extend this technique to query for the rank of a single item:

    ;with cteCount as (
        select ColourID, count(*) as ColourCount
            from Vehicles
            group by ColourID
    ),
    cteRank as (
        select ColourID, ColourCount, 
               row_number() over (order by ColourCount desc) as Rank
            from cteCount
    )
    select ColourID, ColourCount, Rank
        from cteRank
        where ColourID = 10
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sql Server 2008 Express >> Visual Web Developer >> C# I'm pulling records out
(Using SQL Server 2008) I could easily get this to work if I built
In SQL Server 2008 I have a Table- Fruits Items Orders Bananas 6 Bananas
SQL Server 2008 R2 Express Edition. I'm trying to use the import wizard to
Using SQL Server 2008 R2 I am trying to establish a relationship between two
In SQL Server 2008 the isoweek can be found with this: SELECT datepart(iso_week, getdate())
In SQL SERVER 2008 R2, I need to get a count of items contained
SQL Server 2008 Profiler always profiles all databases. I want to restrict profiling or
SQL Server 2008 Database Question. I have 2 tables, for arguments sake called Customers
SQL Server 2008 is telling me to include the Primary Key in an index

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.