From: Clive Swan <cliveswan@gmail.com>
To: 'David G. Johnston' <david.g.johnston@gmail.com>
Cc: 'pgsql-generallists.postgresql.org' <pgsql-general@lists.postgresql.org>
Subject: RE: Get COUNT results from two different columns
Date: Fri, 24 Sep 2021 11:48:00 +0100
Message-ID: <00af01d7b131$a4ee6640$eecb32c0$@gmail.com> (raw)
In-Reply-To: <CAKFQuwYv8TaPnqk=VFn5g4a=47S_RKpkLoLnBGUHCBh1cKS0TA@mail.gmail.com>
References: <002d01d7b080$1d339060$579ab120$@gmail.com>
<CAKFQuwYv8TaPnqk=VFn5g4a=47S_RKpkLoLnBGUHCBh1cKS0TA@mail.gmail.com>
Hi David,
Thanks for the advice much appreciated.
It is the first time that I have encountered this requirement, so was a bit lost in the forest..
As I only want a single id and value (new – old), would it be more efficient to use a SUBTRACT rather than a UNION functions??
The UNION is working now… thanks..
-- UNION
SELECT union_subquery.new_sup_id, sum(new_count)
FROM
(
SELECT
new_sup AS new_sup_id, COUNT(new_sup) AS new_count
FROM public."data"
GROUP BY new_sup_id
UNION
SELECT old_sup,
- COUNT(old_sup) AS old_count
FROM public."data"
GROUP BY old_sup
)
AS union_subquery (new_sup_id, new_count)
GROUP BY union_subquery.new_sup_id, union_subquery.new_count
From: David G. Johnston [mailto:david.g.johnston@gmail.com]
Sent: 23 September 2021 17:45
To: cliveswan@gmail.com
Cc: pgsql-generallists.postgresql.org
Subject: Re: Get COUNT results from two different columns
On Thu, Sep 23, 2021 at 6:37 AM Clive Swan <cliveswan@gmail.com> wrote:
Greetings,
I have two separate queries that work individually, returning a count from each column.
I want to subtract New(COUNT) from Old(Count)
I get an error when trying to run UNION?
While you finally did provide this info, it is important to be upfront about exactly what error you are receiving.
Honestly, it feels odd leveraging UNION to solve this problem, but upon further reflection it does provide a nice solution.
/* an (incomplete) subquery to be substituted into the main query below /*
SELECT id, count(*) AS positive_counts --positive values
UNION ALL
SELECT id, (- count(*)) AS negative_counts --negative of the count
You now have a table where IDs (can) repeat, but at most appear only twice, once with a positive count and once with a negative count. All you need to do to get your final answer is sum the positive and negative count together for each ID.
SELECT union_subquery.id, sum(union_subquery.counted)
FROM (/*this is a subquery, in the FROM clause */) AS union_subquery (id, counted)
-- which requires an alias (name), which your query omitted and why you got the error you described.
-- I added column aliases here to emphasize that there are only two output columns
-- the name of the second column is originally taken from the first unioned query
-- (so, positive_counts, the name negative_counts is discarded once the union is complete.
-- But since writing sum(positive_counts) in the main query would be confusing I renamed
-- the column to just "counted" using the alias clause
GROUP BY union_subquery.id
David J.
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 from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: cliveswan@gmail.com, david.g.johnston@gmail.com
Subject: Re: Get COUNT results from two different columns
In-Reply-To: <00af01d7b131$a4ee6640$eecb32c0$@gmail.com>
References: <002d01d7b080$1d339060$579ab120$@gmail.com> <CAKFQuwYv8TaPnqk=VFn5g4a=47S_RKpkLoLnBGUHCBh1cKS0TA@mail.gmail.com> <00af01d7b131$a4ee6640$eecb32c0$@gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox