SQL - selecting rows with highest group by count

I have a large table with 2 significant columns of data
Part
ApplianceType

In order to set a generic description for each Part, I need to find the most prominent value of ApplianceType.

So in a GROUP BY Part, ApplianceType, I'd want only those rows returned where the COUNT(*) was highest for Part.

What is the best way of doing this?
LVL 1
BeamsonAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pawan KumarDatabase ExpertCommented:
can you please show few rows you are getting?
0
Pawan KumarDatabase ExpertCommented:
You can do like below -


;WITH CTE AS
(
	/*Your code here*/
	/* alias COUNT(*) cnt */
)
SELECT * FROM 
(
	SELECT * , ROW_NUMBER() OVER (PARTITION BY Part ORDER BY cnt DESC) rnk
	FROM CTE
)u WHERE rnk = 1	

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BeamsonAuthor Commented:
For raw data:
Part	ApplianceType
A001	Plasma Television
A001	Plasma Television
A001	LCD Television
A001	Plasma Television
A001	Plasma Television
A001	Plasma Television
A002	DVD Player
A002	HDD/DVD Player
A002	HDD/DVD Player

Open in new window

I'd want to select those rows with the max count of group by:
Part	ApplianceType
A001	Plasma Television
A002	HDD/DVD Player

Open in new window

0
BeamsonAuthor Commented:
Yes, that works - thanks!
1
Pawan KumarDatabase ExpertCommented:
Please find full tested solution for you

Data generation and data insertion

CREATE TABLE MAxGROUP
(
	 Part	VARCHAR(5)
	,ApplianceType VARCHAR(50)
)
GO

INSERT INTO MAxGROUP VALUES
('A001','Plasma Television'),
('A001','Plasma Television'),
('A001','LCD Television'),
('A001','Plasma Television'),
('A001','Plasma Television'),
('A001','Plasma Television'),
('A002','DVD Player'),
('A002','HDD/DVD Player'),
('A002','HDD/DVD Player')
GO

Open in new window


SOLUTION

SELECT Part,ApplianceType
FROM 
(
	SELECT * , ROW_NUMBER() OVER (PARTITION BY Part ORDER BY cnt DESC) rnk
	FROM 
	(
		SELECT Part,ApplianceType,COUNT(*) cnt
		FROM MAxGROUP
		GROUP BY Part,ApplianceType
	)r
)p WHERE rnk = 1

Open in new window


OUTPUT

/*------------------------
OUTPUT
------------------------*/
Part  ApplianceType
----- --------------------------------------------------
A001  Plasma Television
A002  HDD/DVD Player

(2 row(s) affected)

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.