As in PostgreSql to gruppirovanie for null/not_null?

There is then a field, columnName, which can contain either NULL, or some random string. If you write a normal "GROUP BY columnName", then the sample are grouped by each value of columnName that there is almost unique and the meaning of such groupings there. I would like to divide the sample into 2 parts: one where the field value is NULL and one where any other value. Is it possible?
April 19th 20 at 12:26
4 answers
April 19th 20 at 12:28
Solution
Saw 3 answer, well, I think elementary, already answered. But why so difficult?
group by field_name is null
all. The group by expression, why do it row in the subquery or number? bool by itself is more than enough.
Yes, it worked. - Robert.Schuppe commented on April 19th 20 at 12:31
April 19th 20 at 12:30
select count(*),is_null from (
 select case when columnName is null then 'null' else 'not null' end as is_null
 from tableName
) t group by is_null;
And where did t? Do not see these ads alias. - Robert.Schuppe commented on April 19th 20 at 12:33
@Robert.Schuppe, in PostgreSQL you cannot do select ... from (select ...) without specifying the alias of the nested table. - Elian commented on April 19th 20 at 12:36
I just don't see where you have this alias announced. He just appeared out of nowhere. - Robert.Schuppe commented on April 19th 20 at 12:39
@Robert.Schuppe, of course, I myself invented. - Elian commented on April 19th 20 at 12:42
April 19th 20 at 12:32
select group_id, name
from members
where group_id in (
select group_id
from members
group by 1
having bool_and(name is not null)
);

SELECT department, COUNT(*) as “Num of employees” , AVG(salary) as “Avg Dept. Salary”
FROM employee
GROUP BY department
Department ORDER BY NULLS LAST;
April 19th 20 at 12:34
Simple group by expression
group by case when my_field is null then 0 else 1 end

Find more questions by tags SQLPostgreSQL