pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Clive Swan <cliveswan@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: RE: Get COUNT results from two different columns
Date: Thu, 23 Sep 2021 14:37:11 +0100
Message-ID: <002d01d7b080$1d339060$579ab120$@gmail.com> (raw)

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

view thread (7+ messages)  latest in thread

Message-ID: <002d01d7b080$1d339060$579ab120$@gmail.com>
Permalink:  ../002d01d7b080$1d339060$579ab120$@gmail.com/
Also on:    postgresql.org/message-id/002d01d7b080$1d339060$579ab120$@gmail.com

 · 

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 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
  Subject: Re: Get COUNT results from two different columns
  In-Reply-To: <002d01d7b080$1d339060$579ab120$@gmail.com>
  References: <002d01d7b080$1d339060$579ab120$@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