我正在尝试对聚合表进行逆向工程以计算并发值。 表格样本:
start_time end_time duration_s some_id total_val
2023-03-30 01:03:19.000 2023-03-30 01:03:58.699 39.699 A 3098.214
2023-03-30 00:41:50.000 2023-03-30 01:03:59.920 1329.92 A 1264.4286
2023-03-30 01:01:48.000 2023-03-30 01:04:00.759 132.759 B 2323.6356
2023-03-30 00:56:55.000 2023-03-30 01:04:00.946 425.946 B 1676.4208
2023-03-30 01:03:50.000 2023-03-30 01:04:02.711 12.711 A 1211.4553
我正在尝试创建一个按秒分组的连续图。 total_val 是整个持续时间 (end_time - start_time) 的成本,应该平均分配 (total_val/duration_s)。按 start_time 的简单分组不会说明事件何时结束。
我看过类似的questions但我是一个SQL n00b并且会很感激一些指示
这是有效但非常慢的代码(按分钟而不是秒聚合),
WITH temp AS (
SELECT
total_val / duration_s AS usage_per_sec,
SEQUENCE(start_time, end_time, INTERVAL '1' MINUTE) exploded_times
FROM tbl
),
temp2 AS (
SELECT
exploded_times,
TRANSFORM(exploded_times, x -> usage_per_sec * 60) usage_per_min
FROM temp
),
temp3 AS (
SELECT
ts_min,
usage
FROM temp2
CROSS JOIN UNNEST(exploded_times, usage_per_min) AS t (ts_min, usage)
)
SELECT
ts_min,
SUM(usage)
FROM temp3
GROUP BY
ts_min
期望的输出将采用以下格式:ts 和 sum(total_val/duration)
2023-03-29 23:59:56.000 196.93959475378
2023-03-29 23:59:57.000 46216.598554771
2023-03-29 23:59:58.000 57332.587457153
2023-03-29 23:59:59.000 404240.20792671
2023-03-30 00:00:00.000 252365.2397336
2023-03-30 00:00:01.000 665889.58236006
2023-03-30 00:00:02.000 587338.31764963