gpt4 book ai didi

database - 对于 0 个提取的行 postgresql 返回 0 和 null

转载 作者:行者123 更新时间:2023-11-29 14:24:47 25 4
gpt4 key购买 nike

我在下面有以下 SQL 查询,它执行时没有错误,并且可以正常运行,唯一没有做的是当查询返回 0 行时,为 INT 返回 0 列,为 VARCHAR 列返回 NULL 列

查询

SELECT
coalesce((tb_get_customers.customer), 'null') as "CUSTOMER",
coalesce((COUNT(*)),0) as "METRICS",
coalesce((CONCAT('STATUS: ', severity)), 'null') as "SEVERITY"
FROM
tb_get_faults left
JOIN tb_get_customers ON tb_get_faults.customer = tb_get_customers.cust_cmdb
WHERE
tb_get_faults.exception = 'NO'
AND tb_get_faults.type = 'VM'
AND tb_get_faults.customer = 'CLIENT1'
AND tb_get_faults.severity= 'HIGH'
GROUP BY
tb_get_faults.customer,
tb_get_customers.customer,
tb_get_faults.impact
ORDER BY
tb_get_customers.customer ASC;

输出

enter image description here

期望的输出

------------------------------
CUSTOMER | METRICS | SEVERITY
------------------------------
null | 0 | null

最佳答案

使用您的查询创建一个 CTE:

with CTE as (
<your query here>
)
select * from cte
union all
select 'null', 0, 'null'
where not exists (select 1 from cte)

CTE 不返回任何行时,UNION ALL 的第二个查询将返回您期望的行。

关于database - 对于 0 个提取的行 postgresql 返回 0 和 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59089427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com