How to GROUP BY other

How to get top 3 products and all others in a separate row.

Suppose you have a sales table, and you are going to get top 3 quantity wise products and all others in a separate row.

This is our desired result:

Let\’s start with a query which outputs 3 top products:

Join our table with this query:

We almost get desired result, the last field contains numbers of 3 top products and NULL for other. Now we just need to add aggregate functions:

Leave a Reply

Your email address will not be published. Required fields are marked *