RE: Get COUNT results from two different columns

Поиск
Список
Период
Сортировка
От Clive Swan
Тема RE: Get COUNT results from two different columns
Дата
Msg-id 002d01d7b080$1d339060$579ab120$@gmail.com
обсуждение исходный текст
Ответы Re: Get COUNT results from two different columns  (Ray O'Donnell <ray@rodonnell.ie>)
RE: Get COUNT results from two different columns  (SQL Padawan <sql_padawan@protonmail.com>)
Re: Get COUNT results from two different columns  (Marc Olivé <marc.olive@iomed.es>)
Re: Get COUNT results from two different columns  ("David G. Johnston" <david.g.johnston@gmail.com>)
Список pgsql-general

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

В списке pgsql-general по дате отправления:

Предыдущее
От: Ryan Booz
Дата:
Сообщение: Re: Faster distinct query?
Следующее
От: Ray O'Donnell
Дата:
Сообщение: Re: Get COUNT results from two different columns