0
我有这个查询,它根据定义的时间间隔选择每个“消息代码”的计数。根据多个条件构建postgresql查询
SELECT coalesce(count(case when messagecode = 'F-100' then 'F-100' end), 0) as fixed,
coalesce(count(case when messagecode = 'H-100' then 'H-100' end), 0) as hope,
coalesce(count(case when messagecode = 'G-100' then 'G-100' end), 0) as good,
coalesce(count(case when messagecode = 'T-100' then 'T-100' end), 0) as todo
FROM messages WHERE messagetime >= current_timestamp - ('5' * interval '1 minute')
条目
fixed: 115,
hope: 334,
good: 1045,
todo: 6000
我想做出类似的东西,但选择的是具有这种messagecode的独特ID量,我知道我应该做
SELECT coalesce(count(DISTINCT id), 0) ...
开始输出但我的问题是,我还不知道如何在同一个查询中对多个条件执行此操作。预期产出:
fixed: 13, (13 unique ids exist)
hope: 45, (45 unique ids exist)
good: 110, (110 unique ids exist)
todo: 414 (414 unique ids exist)
我将如何能够实现它?
尝试'计数(不同的情况下......然后ID结束)' –
事实上,许多感谢,我从未与ID @then更改消息代码以为会做到这一点。你可能想添加一个答案将接受它。谢谢。 –