在我提供的示例中,我尝试将值重叠的月份的 time_taken 合并为一个月的 2 天。
SELECT
run_date,
max(end_time) - min(start_time) as time_taken
from bpc_bill_proc_cont
where run_date between '01/01/2024' and today
group by 1
order by 1
输出:
03/01/2024 0 00:51:30
04/01/2024 0 01:27:09
04/02/2024 0 01:36:36
03/03/2024 0 00:56:59
04/03/2024 0 01:43:19
03/04/2024 0 00:58:12
04/04/2024 0 01:47:26
03/05/2024 0 00:33:06
04/05/2024 0 01:08:30
04/06/2024 0 01:16:37
03/07/2024 0 00:34:21
04/07/2024 0 01:12:52
03/08/2024 0 00:48:39
04/08/2024 0 01:08:56
我的预期输出必须如下所示:
01/2024 0 02:18:39
02/2024 0 01:36:36
03/2024 0 02:40:18
04/2024 0 02:45:28
05/2024 0 01:41:36
06/2024 0 01:16:37
07/2024 0 01:12:52
08/2024 0 01:57:35
我正在努力如何解决这个问题,需要一些指导。
你能试试这个sql吗?
SELECT TO_CHAR(run_date, 'MM/YYYY') AS month_year,
TO_CHAR(SUM(EXTRACT(EPOCH FROM (max(end_time) - min(start_time)))::INTERVAL), 'HH24:MI:SS') AS time_taken
FROM bpc_bill_proc_cont
WHERE run_date BETWEEN '2024-01-01' AND CURRENT_DATE
GROUP BY TO_CHAR(run_date, 'MM/YYYY')
ORDER BY TO_CHAR(run_date, 'MM/YYYY');