SQL question, TOP 5 and all OTHERS

From: Scott Holliday <scott(dot)holliday(at)simplelegal(dot)com>
To: "pgsql-novice(at)postgresql(dot)org" <pgsql-novice(at)postgresql(dot)org>
Subject: SQL question, TOP 5 and all OTHERS
Date: 2022-06-06 17:22:02
Message-ID: 6D4DD2B0-E735-459E-BE8C-1D63646C5E32@simplelegal.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Hi,

I’m trying to get up-to-speed with PostgreSQL and have a dumb question. I have a basic query to pull the top 5 vendors that have sent me the most bills. I would like to lump all the other vendors into a row named “Other” and get a count of all those bills excluding the top 5. Below is the basic query.

SELECT vendor_name AS vendor_name,
count(DISTINCT inv_id) AS "# of Invoices"
FROM SpendTable
GROUP BY vendor_name
ORDER BY "# of Invoices" DESC
LIMIT 5

Thanks,
Scott

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Jean MAURICE 2022-06-06 19:46:12 Re: SQL question, TOP 5 and all OTHERS
Previous Message Andreas Kretschmer 2022-05-21 13:36:15 Re: Writing my first trigger