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 1066919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:05:01+00:00 2026-05-16T20:05:01+00:00

i have been asked to write a query which in should rank customers base

  • 0

i have been asked to write a query which in should rank customers base on the quantity of orders they have. the main important factor is that if two customers have been ordered the same quantity they should have be in same rank. i want to know the way to handle this query. for this i have began as below:

select tble1.customerid, RANK() OVER (ORDER BY tble1.counts desc) AS [cust grade] from
(select Orders.CustomerID, COUNT(*) as counts  from Orders 
group by CustomerID order by counts desc)tble1

as you have mentioned the output of the table should be some thing like this:
custid1–some–grade
custid2–some–grade
the problem of the code that up is that it skips some numbers. it works well, i mean if two customers have the same quantity of orders it gives them the same grade. but that seams like a queue of sequential numbers that if we have for example three 4s the next grade after them is the 7. but i need to rank it as 5. why this function works in this way. how i can fix it. if it is possible any one can suggest a better query which has a better performance i will be so much pleased.

  • 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-16T20:05:02+00:00Added an answer on May 16, 2026 at 8:05 pm

    You need to use dense_rank() instead of rank() to avoid it skipping numbers.

    ;With tble1 AS
    (
    select Orders.CustomerID, 
    COUNT(*) as counts  
    from Orders 
    group by CustomerID 
    )
    select tble1.customerid, 
    DENSE_RANK() OVER (ORDER BY tble1.counts desc) AS [cust grade] 
    from tble1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been asked in an interview to write a SQL query which fetches
I have been asked to write an app which screen scrapes info from an
I have been asked to write a grid which tracks flexitime for employees. There
I have been asked to write a java program on linux platform. According to
I have been asked to write a GUI that will allow objects to be
I have been asked to write a custom webpage in a web application integrated
I have been asked to lend a hand on a hobby project that a
I have been asked to standardize the screen saver and desktop background used by
I have been asked to develop some usercontrols in ASP.NET that will at a
I have been asked to audit any/all changes in a MySQL table. Does anyone

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.