从时间流中获取每月 sum/agv/max 的测量值

Get monthly sum/agv/max of a measurement from timestream

我正在使用以下属性(示例)在 Timestream 中存储测量值:

@2021-01-04 00:00:00 | DIMENSIONS                 | MEASURES
                     | domain: www.foo.com        | uniq_users: 9
                     | layer: none                | request_count: 11
                     | status: successful         | bytes_sent: 18097
                     | cache_status: Hit          |

让我们假设这些指标每天写一个。我必须如何查询数据才能获得特定域的请求计数指标的每月总和?

为了获得查询的整个时间范围的总和,我可以这样做:

WITH per_day AS (
    SELECT 
       time, 
       domain,
       measure_value::double AS request_count
    FROM 
        "database"."table"
    WHERE 
        time between '2021-01-01 00:00:00' and '2022-01-01 00:00:00'
        AND measure_name = 'request_count'
        AND domain = 'www.foo.com'
    ORDER BY time ASC
)
SELECT sum(request_count)
AS total
FROM per_day

这returns一共指定了范围。有没有办法获得每月的总数(通过 GROUP BY 或类似的方式)?

在大多数 SQL 语言中,您可以使用 date_trunc 将时间戳截断为不同的精度,AWS Timestream included

在您的情况下,您希望将 time 截断为每月精度:date_trunc('month', time)。然后,您可以按此变量分组并求和 request_count.

SELECT 
   date_trunc('month', time) AS month, 
   SUM(measure_value::double) AS request_count
FROM 
    "database"."table"
WHERE 
    time between '2022-01-01 00:00:00' and '2022-01-02 00:00:00'
    AND measure_name = 'request_count'
    AND domain = 'www.foo.com'
GROUP BY 1
ORDER BY month ASC