From: | Marc Olivé <marc(dot)olive(at)iomed(dot)es> |
---|---|
To: | pgsql-general(at)lists(dot)postgresql(dot)org |
Subject: | Re: Get COUNT results from two different columns |
Date: | 2021-09-23 14:20:34 |
Message-ID: | CAB7_X5wUaCWDhS698BfmuADJ5OmBuxssvPT_zvuD_4zkOfom7w@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
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(at)gmail(dot)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
>
From | Date | Subject | |
---|---|---|---|
Next Message | Clive Swan | 2021-09-23 15:16:28 | RE: Get COUNT results from two different columns |
Previous Message | SQL Padawan | 2021-09-23 13:50:20 | RE: Get COUNT results from two different columns |