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
I'd want to select those rows with the max count of group by:Part ApplianceType
A001 Plasma Television
A002 HDD/DVD Player
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
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
/*------------------------
OUTPUT
------------------------*/
Part ApplianceType
----- --------------------------------------------------
A001 Plasma Television
A002 HDD/DVD Player
(2 row(s) affected)