限制每行返回的最大值

Limit the maximum value returned for each row

假设我有一个 table 如下:

|id | duration | timeout
=========================
| 1 |   100    |   0
| 2 |  12345   |   1
| 3 |   321    |   0

我想获得总持续时间,以及会话数和超时数,如下所示:

SELECT count(id) session_count, sum(duration) duration, count(timeout) timeouts

但是 对于任何超时的会话,我需要将值限制为 1000。所以我的查询应该生成如下内容:

| session_count | duration | timeouts
=====================================
|     3         |  1421    |    1

提前致谢。

给你,也将 count(timeout) 更改为 sum(timeout)if 所做的是检查超时值,如果 0 则使用持续时间,否则使用 1000

SELECT COUNT(id) session_count, SUM(IF (timeout = 0, duration,1000)) duration, SUM(timeout) timeouts