Пропустить всю строку, если агрегированное значение равно нулю - PullRequest
0 голосов
/ 07 декабря 2018

Это мой подход:

select distinct (invoice_no) as no,sum(total),
                sum(case when department_id=2 then total end) as a2,
                sum(case when department_id=3 then total end) as a3,
                sum(case when department_id=4 then total end) as a4,
                sum(case when department_id=5 then total end) as a5,
                sum(case when department_id=6 then total end) as a6
from article_sale
where  invoice_date = '2018-10-01' group by no order by no ASC

Запрос возвращает вывод, подобный следующему:

no      sum a2      a3    a4    a5      a6
68630   690 NULL    75    404   NULL    210.8
68631   0   NULL    NULL  NULL  NULL    NULL
68632   132 NULL    45    87    NULL    NULL
68633   75  NULL    75    NULL  NULL    NULL
68634   523 NULL    130   NULL  NULL    392.55
68635   0   NULL    NULL  NULL  NULL    NULL
68636   310 NULL    NULL  218   NULL    91.91
68637   273 NULL    NULL  NULL  NULL    273.24
68638   0   NULL    NULL  NULL  NULL    NULL

Я хочу получить только строки, где a6 равно NOT NULL.Остальные строки должны быть отфильтрованы.
Желаемый результат:

no      sum a2      a3    a4    a5      a6
68630   690 NULL    75    404   NULL    210.8
68634   523 NULL    130   NULL  NULL    392.55
68636   310 NULL    NULL  218   NULL    91.91
68637   273 NULL    NULL  NULL  NULL    273.24

Как лучше всего добиться этого?

Ответы [ 3 ]

0 голосов
/ 07 декабря 2018

добавить HAVING a6 IS NOT NULL после группировки по.Таким образом, запрос станет

select distinct (invoice_no) as no,sum(total), 
sum(case when department_id=2 then total end) as a2, 
sum(case when department_id=3 then total end) as a3, sum(case when department_id=4 then total end) as a4, 
sum(case when department_id=5 then total end) as a5, 
sum(case when department_id=6 then total end) as a6 from article_sale where invoice_date = '2018-10-01' 
group by no having sum(case when department_id=6 then total end) is not null order by no ASC
0 голосов
/ 07 декабря 2018

Добавьте предложение HAVING:

SELECT invoice_no                                  AS no
     , sum(total)                                  AS sum_total
     , sum(total) FILTER (WHERE department_id = 2) AS a2
     , sum(total) FILTER (WHERE department_id = 3) AS a3
     , sum(total) FILTER (WHERE department_id = 4) AS a4
     , sum(total) FILTER (WHERE department_id = 5) AS a5
     , sum(total) FILTER (WHERE department_id = 6) AS a6
FROM   article_sale
WHERE  invoice_date = '2018-10-01'
GROUP  BY 1
HAVING sum(total) FILTER (WHERE department_id = 6) IS NOT NULL
ORDER  BY 1;

Но сначала отбросьте лишнюю, дорогую DISTINCT.Строки должны быть различны после применения GROUP BY.Также не путайте DISTINCT (invoice_no) с DISTINCT ON (invoice_no).Первый содержит вводящие в заблуждение круглые скобки, которые удаляются.Второй имеет другое значение.См .:

Также используется современное, более быстрое предложение FILTER для ваших условных агрегатов.См .:

0 голосов
/ 07 декабря 2018

Если вы хотите отфильтровать нулевые значения, вы можете поместить AND a6 IS NOT NULL в ваше условие WHERE

...