我有一个PostgreSQL类型和一个表
CREATE TYPE mem_status AS ENUM('waiting', 'active', 'expired');
CREATE TABLE mems (
id BIGSERIAL PRIMARY KEY,
status mem_status NOT NULL
);
数据集
INSERT INTO mems(id, status) VALUES
(1, 'active'), (2, 'active'), (3, 'expired');
我想查询按状态分组的计数。所以我在下面查询。
WITH mem_statuses AS (
SELECT unnest(enum_range(NULL::mem_status)) AS status
)
SELECT m.status, count(1)
FROM mems m
RIGHT JOIN mem_statuses ms ON ms.status = m.status
GROUP BY m.status;
但是如果没有waiting
内存,则结果如下所示。
status | count
================
NULL | 1 <- problem
'active' | 2
'expired' | 1
我想要这样的结果。
status | count
================
'waiting' | 0
'active' | 2
'expired' | 1
我该怎么做?
答案 0 :(得分:0)
使用count(id)
:
WITH mem_statuses AS (
SELECT unnest(enum_range(NULL::mem_status)) AS status
)
SELECT ms.status, count(id)
FROM mems m
RIGHT JOIN mem_statuses ms ON ms.status = m.status
GROUP BY ms.status;
或:
select status, count(id)
from unnest(enum_range(null::mem_status)) as status
left join mems using(status)
group by status
status | count
---------+-------
waiting | 0
active | 2
expired | 1
(3 rows)
每个the documentation count(expression)
给出
表达式值不为空的输入行数
答案 1 :(得分:0)
您需要修改联接并进行一些汇总-
- set_fact:
tagExists: "{{ true if apiCheckResult.json|json_query('values[?name == `myDemo`]') else false }}"