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 1mTikT-0001BA-Vx for pgsql-general@arkaria.postgresql.org; Fri, 24 Sep 2021 10:48:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mTikS-0006O4-5C for pgsql-general@arkaria.postgresql.org; Fri, 24 Sep 2021 10:48:08 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mTikR-0006Nv-GI for pgsql-general@lists.postgresql.org; Fri, 24 Sep 2021 10:48:07 +0000 Received: from mail-wr1-x42e.google.com ([2a00:1450:4864:20::42e]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mTikO-0007dm-NN for pgsql-general@lists.postgresql.org; Fri, 24 Sep 2021 10:48:06 +0000 Received: by mail-wr1-x42e.google.com with SMTP id i24so10159452wrc.9 for ; Fri, 24 Sep 2021 03:48:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=reply-to:from:to:cc:references:in-reply-to:subject:date:message-id :mime-version:thread-index:content-language; bh=Lly4lKa5UX1lIoN4l1TVcTRNsFmTqDZjywneamlMJhI=; b=lHcGlCtCkLL7t2Rqe4FJSyQ9OO5tFOXH8Uw3qR3wXK+z8D/Xvxk6xJ+9R0K3aixVZJ py8kLYudXLWIPWX9uqrsQpCUpdDfA6t4/+LrSvuUpUKAq+J5l9AYyBUF+s0i07At1JAv NLRx7qLJrrZfsPflCPjA0pmvJTvxIiuqo2F9d+I7hkMMQ6YR3f46y7O7Sr+JZVeuJ9yA RAP256RPFGliGModOvQSyHE+aWQE2YZ905efIdfQJuWguPW1nnZN/9C9C8l27/tjL817 q5/qQKMMY4las/LUVHHVE/3CBm8icVQKvzqyr2eqDXfLtDmIY1UMS+EEp8brrxhnbryg bq8Q== 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:cc:references:in-reply-to :subject:date:message-id:mime-version:thread-index:content-language; bh=Lly4lKa5UX1lIoN4l1TVcTRNsFmTqDZjywneamlMJhI=; b=VnjdlKRP0R6hhNGI2nXSEE+aDL0H2xBHQP00ZoYAmMA4zAjVaS/uvQ6gG7ELTjifMF heMxx1TEXUHgmKJTuik4F82aX+k4S7mMTk59w6jsdcx2uqKJ2ZtvMdkbiWXFaCVP2B1D VSYoPGNvdwfn8zZUzVJJTTKvhaCqL0zhz24+/cyYXKOEvOsYHAMNg826wcGMCP/pUMNo 0XcR4a05gqK5UbVig/mNd5kMAstwRCc5GPewuZ8dmEjU8/KETD6h+Xx8oNUl/MPUxErz lQZNDPoFmpQwudd0adIc6QPC46gHOzQ5+pkgxlI5Xe0bODG3JQsG51EMYpfCJvGxRuKd dgsg== X-Gm-Message-State: AOAM532Z2wxiIWO3BYQYHSLf/rt3pdrz0DQe9pmemXbJusjUG3FGA7gy E/uNs6RLAgj+IX3+n03ZPb2xCLkj00c= X-Google-Smtp-Source: ABdhPJwJ3gIUrXAsENXDeFBk8Tzm1eQ2ySSoLDi5sJcZbJjB6Dex+Sk/HYAaNtldHko0AINZcE3QZA== X-Received: by 2002:a7b:c148:: with SMTP id z8mr1312200wmi.160.1632480482205; Fri, 24 Sep 2021 03:48:02 -0700 (PDT) Received: from DESKTOPDJK5SEF ([2a00:23c5:4210:1d01:964:998c:55b7:6306]) by smtp.gmail.com with ESMTPSA id c18sm322176wmb.27.2021.09.24.03.48.00 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 24 Sep 2021 03:48:00 -0700 (PDT) Reply-To: From: "Clive Swan" To: "'David G. Johnston'" Cc: "'pgsql-generallists.postgresql.org'" References: <002d01d7b080$1d339060$579ab120$@gmail.com> In-Reply-To: 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> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_00B0_01D7B13A.06B406C0" X-Mailer: Microsoft Outlook 14.0 Thread-Index: AQFraaHSipoifrbfbFMrAzNmSsVIDgGHoMstrH9RL7A= 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_00B0_01D7B13A.06B406C0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Hi David, =20 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.. =20 As I only want a single id and value (new =E2=80=93 old), would it be = more efficient to use a SUBTRACT rather than a UNION functions?? =20 The UNION is working now=E2=80=A6 thanks.. =20 =20 -- UNION=20 SELECT union_subquery.new_sup_id, sum(new_count) =20 FROM ( SELECT=20 new_sup AS new_sup_id, COUNT(new_sup) AS new_count FROM public."data"=20 GROUP BY new_sup_id =20 UNION =20 SELECT old_sup,=20 - 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 =20 =20 =20 From: David G. Johnston [mailto:david.g.johnston@gmail.com]=20 Sent: 23 September 2021 17:45 To: cliveswan@gmail.com Cc: pgsql-generallists.postgresql.org Subject: Re: Get COUNT results from two different columns =20 On Thu, Sep 23, 2021 at 6:37 AM Clive Swan wrote: Greetings, I have two separate queries that work individually, returning a count = from each column. =20 I want to subtract New(COUNT) from Old(Count) =20 I get an error when trying to run UNION? =20 While you finally did provide this info, it is important to be upfront = about exactly what error you are receiving. =20 Honestly, it feels odd leveraging UNION to solve this problem, but upon = further reflection it does provide a nice solution. =20 /* 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 =20 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. =20 SELECT union_subquery.id, sum(union_subquery.counted) =20 FROM (/*this is a subquery, in the FROM clause */) AS union_subquery = (id, counted) =20 -- which requires an alias (name), which your query omitted and why you = got the error you described. =20 -- 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 =20 GROUP BY union_subquery.id =20 David J. =20 ------=_NextPart_000_00B0_01D7B13A.06B406C0 Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable

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 =E2=80=93 old), would it be = more efficient to use a SUBTRACT rather than a UNION = functions??

 

The UNION is working now=E2=80=A6 thanks..

 

 

-- UNION

SELECT union_subquery.new_sup_id, = sum(new_count)

 

FROM

(

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0 SELECT

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0 new_sup AS new_sup_id, COUNT(new_sup) AS = new_count

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0 FROM public."data"

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0 GROUP BY new_sup_id

 

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0 UNION

 

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0 SELECT old_sup,

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0 - COUNT(old_sup) AS old_count

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0 FROM public."data"

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0 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.

 <= /o:p>

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

 

 

David = J.

 

------=_NextPart_000_00B0_01D7B13A.06B406C0--