获取时间戳之间的行数作为列表

Get the number of rows between timestamps as a list

我有一个名为 Table 的 sql 数据库 table,其中包含以下列。如图所示,我想获得每个时间戳之间的计数。

来自这个命令

Select count(*)
from Table
where orderId=500
and (timestamp<=t1 and timestamp>=t2)
or (timestamp<=t3 and timestamp>=t4);

我能够获得时间戳之间的总计数。我想知道如何将每个时间戳条件之间的计数作为列表。

您在寻找条件聚合吗?

Select sum(timestamp <= t1 and timestamp >= t2),
       sum(timestamp <= t3 and timestamp >= t4)
from Table
where orderId = 500;

注意:以上是 MySQL 特有的语法。更通用的语法是:

Select sum(case when timestamp <= t1 and timestamp >= t2 then 1 else 0 end),
       sum(case when timestamp <= t3 and timestamp >= t4 then 1 else 0 end)
from Table
where orderId = 500;