pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedRE: Get COUNT results from two different columns
7+ messages / 5 participants
[nested] [flat]
* RE: Get COUNT results from two different columns
@ 2021-09-23 13:37 Clive Swan <cliveswan@gmail.com>
2021-09-23 13:47 ` Re: Get COUNT results from two different columns Ray O'Donnell <ray@rodonnell.ie>
2021-09-23 13:50 ` RE: Get COUNT results from two different columns SQL Padawan <sql_padawan@protonmail.com>
2021-09-23 14:20 ` Re: Get COUNT results from two different columns Marc Olivé <marc.olive@iomed.es>
2021-09-23 16:44 ` Re: Get COUNT results from two different columns David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 4 replies; 7+ messages in thread
From: Clive Swan @ 2021-09-23 13:37 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
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?
I would appreciate any pointers.
-- COUNT NEW SUPPLIER
--
SELECT new_sup,
COUNT(new_sup)
FROM public."Data"
GROUP BY new_sup
This returns
Company_D 35
Company_E 30
Company_F 30
SELECT newld_sup,
COUNT(old_sup)
FROM public."Data"
GROUP BY old_sup
This returns
Company_A 1
Company_B 2
Company_D 35
Company_E 30
Company_F 30
SELECT new_sup, COUNT(new_sup) AS new_sup_count,
old_sup, COUNT(old_sup) AS old_sup_count
FROM
(
SELECT
new_sup, COUNT(new_sup) AS new_sup_count
FROM public."Data"
GROUP BY new_sup
UNION
SELECT
old_sup, COUNT(old_sup) AS old_sup_count
FROM public."Data"
GROUP BY old_sup
new_sup_count - old_sup_count
)
GROUP BY new_sup
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Get COUNT results from two different columns
2021-09-23 13:37 RE: Get COUNT results from two different columns Clive Swan <cliveswan@gmail.com>
@ 2021-09-23 13:47 ` Ray O'Donnell <ray@rodonnell.ie>
3 siblings, 0 replies; 7+ messages in thread
From: Ray O'Donnell @ 2021-09-23 13:47 UTC (permalink / raw)
To: cliveswan@gmail.com; pgsql-general@lists.postgresql.org
On 23/09/2021 14:37, Clive Swan 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?
At a quick guess, you'll need to move the subtraction outside the two
subqueries - stuff inside either query can't see anything in the other one.
Ray.
--
Raymond O'Donnell // Galway // Ireland
ray@rodonnell.ie
^ permalink raw reply [nested|flat] 7+ messages in thread
* RE: Get COUNT results from two different columns
2021-09-23 13:37 RE: Get COUNT results from two different columns Clive Swan <cliveswan@gmail.com>
@ 2021-09-23 13:50 ` SQL Padawan <sql_padawan@protonmail.com>
2021-09-23 15:16 ` RE: Get COUNT results from two different columns Clive Swan <cliveswan@gmail.com>
3 siblings, 1 reply; 7+ messages in thread
From: SQL Padawan @ 2021-09-23 13:50 UTC (permalink / raw)
To: cliveswan@gmail.com; +Cc: pgsql-general@lists.postgresql.org
Hi,
> I have two separate queries that work individually, returning a count from each column.
> I would appreciate any pointers.
> new_sup, COUNT(new_sup) AS new_sup_count
> old_sup, COUNT(old_sup) AS old_sup_count
At least some of your problem is here - in your UNION-ed table, this is one and the same field and therefore cannot have two different names.
HTH,
SQL Padawan
^ permalink raw reply [nested|flat] 7+ messages in thread
* RE: Get COUNT results from two different columns
2021-09-23 13:37 RE: Get COUNT results from two different columns Clive Swan <cliveswan@gmail.com>
2021-09-23 13:50 ` RE: Get COUNT results from two different columns SQL Padawan <sql_padawan@protonmail.com>
@ 2021-09-23 15:16 ` Clive Swan <cliveswan@gmail.com>
0 siblings, 0 replies; 7+ messages in thread
From: Clive Swan @ 2021-09-23 15:16 UTC (permalink / raw)
To: 'SQL Padawan' <sql_padawan@protonmail.com>; +Cc: pgsql-general@lists.postgresql.org
Greetings,
Thanks for the message.
The error message that I get is.
ERROR:
subquery in FROM must have an alias
Hint: For example, FROM (SELECT ...) [AS] foo.
Position: 100
Trying to make sense of it..
Clive
-----Original Message-----
From: SQL Padawan [mailto:sql_padawan@protonmail.com]
Sent: 23 September 2021 14:50
To: cliveswan@gmail.com
Cc: pgsql-general@lists.postgresql.org
Subject: RE: Get COUNT results from two different columns
Hi,
> I have two separate queries that work individually, returning a count from each column.
> I would appreciate any pointers.
> new_sup, COUNT(new_sup) AS new_sup_count
> old_sup, COUNT(old_sup) AS old_sup_count
At least some of your problem is here - in your UNION-ed table, this is one and the same field and therefore cannot have two different names.
HTH,
SQL Padawan
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Get COUNT results from two different columns
2021-09-23 13:37 RE: Get COUNT results from two different columns Clive Swan <cliveswan@gmail.com>
@ 2021-09-23 14:20 ` Marc Olivé <marc.olive@iomed.es>
3 siblings, 0 replies; 7+ messages in thread
From: Marc Olivé @ 2021-09-23 14:20 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
Seems to me that's not an UNION, but a JOIN:
SELECT o.old_count - n.new_count, o.old_sup, n.new_sup
FROM (
SELECT new_sup, COUNT(new_sup)
FROM public."Data"
GROUP BY new_sup
) n
JOIN (
SELECT old_sup, COUNT(old_sup)
FROM public."Data"
GROUP BY old_sup
) o ON o.old_sup = n.new_sup -- I'm gessing this is the join condition you
want
;
Regards,
El jue, 23 sept 2021 a las 15:37, Clive Swan (<cliveswan@gmail.com>)
escribió:
> 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?
>
>
>
> I would appreciate any pointers.
>
>
>
>
>
> -- COUNT NEW SUPPLIER
>
> --
>
> SELECT new_sup,
>
> COUNT(new_sup)
>
> FROM public."Data"
>
> GROUP BY new_sup
>
>
>
> This returns
>
> Company_D 35
>
> Company_E 30
>
> Company_F 30
>
>
>
>
>
> SELECT newld_sup,
>
> COUNT(old_sup)
>
> FROM public."Data"
>
> GROUP BY old_sup
>
>
>
> This returns
>
> Company_A 1
>
> Company_B 2
>
> Company_D 35
>
> Company_E 30
>
> Company_F 30
>
>
>
>
>
>
>
> SELECT new_sup, COUNT(new_sup) AS new_sup_count,
>
> old_sup, COUNT(old_sup) AS old_sup_count
>
> FROM
>
> (
>
> SELECT
>
> new_sup, COUNT(new_sup) AS new_sup_count
>
> FROM public."Data"
>
> GROUP BY new_sup
>
>
>
> UNION
>
>
>
> SELECT
>
> old_sup, COUNT(old_sup) AS old_sup_count
>
> FROM public."Data"
>
> GROUP BY old_sup
>
>
>
> new_sup_count - old_sup_count
>
> )
>
> GROUP BY new_sup
>
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Get COUNT results from two different columns
2021-09-23 13:37 RE: Get COUNT results from two different columns Clive Swan <cliveswan@gmail.com>
@ 2021-09-23 16:44 ` David G. Johnston <david.g.johnston@gmail.com>
2021-09-24 10:48 ` RE: Get COUNT results from two different columns Clive Swan <cliveswan@gmail.com>
3 siblings, 1 reply; 7+ messages in thread
From: David G. Johnston @ 2021-09-23 16:44 UTC (permalink / raw)
To: cliveswan@gmail.com; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
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.
^ permalink raw reply [nested|flat] 7+ messages in thread
* RE: Get COUNT results from two different columns
2021-09-23 13:37 RE: Get COUNT results from two different columns Clive Swan <cliveswan@gmail.com>
2021-09-23 16:44 ` Re: Get COUNT results from two different columns David G. Johnston <david.g.johnston@gmail.com>
@ 2021-09-24 10:48 ` Clive Swan <cliveswan@gmail.com>
0 siblings, 0 replies; 7+ messages in thread
From: Clive Swan @ 2021-09-24 10:48 UTC (permalink / raw)
To: 'David G. Johnston' <david.g.johnston@gmail.com>; +Cc: 'pgsql-generallists.postgresql.org' <pgsql-general@lists.postgresql.org>
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.
^ permalink raw reply [nested|flat] 7+ messages in thread
end of thread, other threads:[~2021-09-24 10:48 UTC | newest]
Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-09-23 13:37 RE: Get COUNT results from two different columns Clive Swan <cliveswan@gmail.com>
2021-09-23 13:47 ` Ray O'Donnell <ray@rodonnell.ie>
2021-09-23 13:50 ` SQL Padawan <sql_padawan@protonmail.com>
2021-09-23 15:16 ` Clive Swan <cliveswan@gmail.com>
2021-09-23 14:20 ` Marc Olivé <marc.olive@iomed.es>
2021-09-23 16:44 ` David G. Johnston <david.g.johnston@gmail.com>
2021-09-24 10:48 ` Clive Swan <cliveswan@gmail.com>
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