Avatar of Mehawitchi
Mehawitchi

asked on 

How to Club Rows in One Row As Others - SQL Server

Hello experts,

The following SQL query yields Results #1 (see below)

SELECT Top 10 ChGroupName AS GROUPNAME, SUM(SCORE) AS SCORE, RANK() OVER(ORDER BY SUM(SCORE) desc) AS RANKING
FROM SmryTable  
GROUP BY ChGroupName
ORDER BY RANKING


I need to alter the above query to make it list top 4 and club entries from 5 onwards as OTHERS, so that the results will look like the following:
User generated image
Thanks for your help,
Hani
Microsoft SQL ServerSQL

Avatar of undefined
Last Comment
Mike Eghtebas
ASKER CERTIFIED SOLUTION
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of Mlanda T
Mlanda T
Flag of South Africa image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Mehawitchi
Mehawitchi

ASKER

Thanks eghtebas - This looks promising.

I will test and get back to you with feedback.

Best,
Hani
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

Line 19 has typo. Please change

FROM ce2;

to

FROM cte2;
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo