Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mTOuc-0003D3-MA for pgsql-general@arkaria.postgresql.org; Thu, 23 Sep 2021 13:37:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mTOub-0006oS-MD for pgsql-general@arkaria.postgresql.org; Thu, 23 Sep 2021 13:37:17 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mTOub-0006oK-C1 for pgsql-general@lists.postgresql.org; Thu, 23 Sep 2021 13:37:17 +0000 Received: from mail-wr1-x42d.google.com ([2a00:1450:4864:20::42d]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mTOuY-0005KB-Qd for pgsql-general@lists.postgresql.org; Thu, 23 Sep 2021 13:37:17 +0000 Received: by mail-wr1-x42d.google.com with SMTP id g16so17335295wrb.3 for ; Thu, 23 Sep 2021 06:37:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=reply-to:from:to:subject:date:message-id:mime-version:thread-index :content-language; bh=XsKiAU+QO1upa56Kh3EZv0KnglzNYrN0cAm1/ueX0MU=; b=ZjThUmVq3MxZDyjcnfNBTlhfPwOp9+Pqn2cdGekJYClBSLUR1DpkDMTrduZHXwqnIB aagDEd2pQml5HTdWmHDm9TR2HK8dzDiNmUwDeixR1o5F4MXAOIyRd8faUG8+CLz3vCQh cFY/6WnmMVrBntNmW4Y/1yLkvX/GAtn5XKNh/bI5rzue1a6sLkHLCqxSDIBKi37WM5c+ br5PvZN9kOwvwafWZI+8Z9cftWIdEQeUePsw1tQsCVsH0a0rfm7E2f1TE1WIOe37IEBf rCcaapLtGI7Y1C2+L5sXmZ3JZQ0P8vXs2qp/ELneZl45cIj0HE4BrmN4y+fi491AOF0q c+ZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:reply-to:from:to:subject:date:message-id :mime-version:thread-index:content-language; bh=XsKiAU+QO1upa56Kh3EZv0KnglzNYrN0cAm1/ueX0MU=; b=SAE3fi+PkKQYR5JME6CjYLoolSD1s9e1Jt1FwSGKJxLedNWrj+a/XoPeR7veSi1kp/ NBUDlqiSzrYuLFlGS30tbFfkkPxc7oLJsZnVtoIpZoUzf8Qjm2OQ1vyN0EvCWErPXx3J wUyXOSGFul6W762D20sYEzArjVqIbwokutweDzSueFjB6PXRaWEem4gWg+PRN1F50kUW 6fCai4A5RSs+V/SC2ZdtzNoeEWQUh/MQbYzREtm2xjL2aots/Lu+6iQBA/LIW6YGoLL7 K1Ys+aTxfUHKzzaU17ERC0CqKDdnd4aYNuqCKRFSW3wISf8P1AUoOALaDQJ58PReGshf rUZQ== X-Gm-Message-State: AOAM530tIz8G/kl+PGKp4qusvWFUOLxjO/fUwzUMn3K+tqkG7qTckV/L QrGebHyK59g8WK3P+RhCBwFzD2I5cXQ= X-Google-Smtp-Source: ABdhPJx/C5OzqfvQ8SZllKF95uVwBfB1zSyDdlI5abtGigXW+ll+i4+mUXJvOEVK75X8boHEssitwg== X-Received: by 2002:a05:600c:b42:: with SMTP id k2mr16317449wmr.134.1632404233170; Thu, 23 Sep 2021 06:37:13 -0700 (PDT) Received: from DESKTOPDJK5SEF ([2a00:23c5:4210:1d01:f000:9cf0:2fa6:1351]) by smtp.gmail.com with ESMTPSA id j27sm4725931wms.6.2021.09.23.06.37.11 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 23 Sep 2021 06:37:12 -0700 (PDT) Reply-To: From: "Clive Swan" To: 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> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_002E_01D7B088.7EF8BBB0" X-Mailer: Microsoft Outlook 14.0 Thread-Index: AdewgBxJXmcySSqhSp2SfUpwrPqAwA== Content-Language: en-gb List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multipart message in MIME format. ------=_NextPart_000_002E_01D7B088.7EF8BBB0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit 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 ------=_NextPart_000_002E_01D7B088.7EF8BBB0 Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable

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

(

         &= nbsp;      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

         &= nbsp;      )

GROUP BY new_sup

------=_NextPart_000_002E_01D7B088.7EF8BBB0--