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更改消息代碼以爲會做到這一點。你可能想添加一個答案將接受它。謝謝。 –