SQL question, TOP 5 and all OTHERS - Mailing list pgsql-novice

From Scott Holliday
Subject SQL question, TOP 5 and all OTHERS
Date
Msg-id 6D4DD2B0-E735-459E-BE8C-1D63646C5E32@simplelegal.com
Whole thread Raw
Responses Re: SQL question, TOP 5 and all OTHERS
List 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

pgsql-novice by date:

Previous
From: Andreas Kretschmer
Date:
Subject: Re: Writing my first trigger
Next
From: Jean MAURICE
Date:
Subject: Re: SQL question, TOP 5 and all OTHERS