public inbox for [email protected]
help / color / mirror / Atom feedFrom: Skylar Thompson <[email protected]>
To: Jean MAURICE <[email protected]>
Cc: [email protected]
Subject: Re: SQL question, TOP 5 and all OTHERS
Date: Mon, 6 Jun 2022 14:12:13 -0700
Message-ID: <20220606211213.dntvfnkwauk52fzc@thargelion> (raw)
In-Reply-To: <[email protected]>
References: <[email protected]>
<[email protected]>
<[email protected]>
On Mon, Jun 06, 2022 at 09:46:12PM +0200, Jean MAURICE wrote:
> Hi Scott,
> what about using a Common Table Expression and the clause WITH ?
> I am not at home now but you can write something like
>
> WITH top5 AS (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)
> SELECT * FROM top5
> UNION
>
> SELECT 'all other' AS vendor_name,
>
> ?????? count(DISTINCT st.inv_id) AS "# of Invoices"
>
> FROM SpendTable AS st
>
> WHERE st.vendor_name NOT IN (SELECT vendor_name FROM top5)
>
> ORDER BY "# of Invoices" DESC
There might be a challenge with ties, especially if you don't order by the
vendor name since you could get different results even on the same data
set, depending on how the query plan goes. It depends on what the OP is
looking for, I guess.
--
-- Skylar Thompson ([email protected])
-- Genome Sciences Department (UW Medicine), System Administrator
-- Foege Building S046, (206)-685-7354
-- Pronouns: He/Him/His
view thread (3+ messages)
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: [email protected]
Cc: [email protected], [email protected], [email protected]
Subject: Re: SQL question, TOP 5 and all OTHERS
In-Reply-To: <20220606211213.dntvfnkwauk52fzc@thargelion>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox