1

I am doing the following SELECT statement to see how many Reps each of my Manufacturers have in my database, this is working nicely:

SELECT ManufacturerId, COUNT(*) AS RepCount 
FROM ManufacturerSalesReps WHERE IsDeleted=0
GROUP BY ManufacturerID
ORDER BY  COUNT(*) 

So this gives me the ManufacturerId and the number of reps but what i really need is the number of manufacturers at different rep counts. So I want to GROUP the results from the above SELECT by RepCount.

How do I accomplish this?

2 Answers 2

2

I cannot think of something else, but:

SELECT T.RepCount, COUNT(*) AS ManufacturerCount
FROM (
    SELECT ManufacturerId, COUNT(*) AS RepCount 
    FROM ManufacturerSalesReps
    WHERE IsDeleted=0
    GROUP BY ManufacturerID
) AS T
GROUP BY T.RepCount
ORDER BY  COUNT(*) 

Either this is correct or totally dumb.

Sign up to request clarification or add additional context in comments.

Comments

0

With windowing functions

SELECT ManufacturerId, COUNT(*), 
count(ManufacturerId) over (partition by COUNT(*)) num_man AS RepCount 
FROM ManufacturerSalesReps WHERE IsDeleted=0
GROUP BY ManufacturerID
ORDER BY  2 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.