我正在嘗試計算或計算日期差異為一定數量但不確定如何計算的行數
select count(case when date_diff('day', min_time, max_time) <= 7 then 1 else null end) / count(*)
from table
但它似乎并沒有做到這一點,不確定是否有更好的方法來實作這一點,謝謝!
uj5u.com熱心網友回復:
編輯:正如@shawnt00 在對該問題的評論中提到的那樣,問題很可能是因為您正在除以整數(也導致整數,即在這種情況下為 0 或 1)。添加 a* 1.0應該可以解決問題(或者如果您愿意,可以將其轉換為 float8 )。
您可以FILTER為此使用該子句:
select count(*) filter (where date_diff('day', min_time, max_time) <= 7) * 1.0 / count(*)
from table
雖然看起來你正在計算平均值,但這樣的事情可能是最簡單的:
select avg(case when date_diff('day', min_time, max_time) <= 7 then 1.0 else 0.0 end)
from table
uj5u.com熱心網友回復:
為什么不使用count(*) 帶有 where 子句的 a 來選擇行?
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/331685.html
標籤:sql PostgreSQL
上一篇:插入回傳“上次操作沒有產生結果”
