How can one row report both request volume and timeouts?
An aggregate FILTER restricts the rows entering one aggregate. Other aggregates in the query can still use the entire input.
SQLite example and interpretation
One of three requests times out. Putting that condition in WHERE would remove both successes from the volume denominator.
WITH r(status) AS (VALUES('ok'),('timeout'),('ok')) SELECT count(*),count(*) FILTER (WHERE status='timeout') FROM r;Result [[3, 1]]
中文
如何在同一行报告总请求和超时请求?
聚合函数的 FILTER 子句只筛选进入该函数的行,同一查询里的其他聚合仍能看到全集。
SQLite 示例与适用条件
三次请求有一次超时。将条件写在 WHERE 会把两次成功请求从总量分母里一起删掉。
WITH r(status) AS (VALUES('ok'),('timeout'),('ok')) SELECT count(*),count(*) FILTER (WHERE status='timeout') FROM r;结果 [[3, 1]]