将时间序列数据转换为日期周期列(汇总求和)

huangapple go评论132阅读模式
英文:

Pivot time series into date period columns (sum aggregation)

问题

很抱歉,我不是数据库专家,但我会尽力帮助你重新构建 PostgreSQL 表。你想要的是将充电时间汇总,以便在行中包含所有ID,列中包含二月份的所有日期(从2/1/23到2/28/23)。每天的充电时间总和将填充在相应的单元格中。

举例:

evse_id 2023-02-01 2023-02-02
DEBDOE7161977714 30 0
DEICTE0001940 15 0
DEWITE0054 15 0
DEBPEE0F5540103 15 0
DEONEEJK5A 0 30
DEVKWE4013 0 15

希望这对你有所帮助。 将时间序列数据转换为日期周期列(汇总求和)

英文:

Unfortunately I am not a database expert and I am trying to rebuild a PostgreSQL table as follows.

I have many many rows with charge points (which have a corresponding ID). Beside this I have a date incl. time and a charging duration (always 15 minutes). I would like to sum these load times so that I have all the IDs in the rows and all the days of February (2/1/23 to 2/28/23) in the columns. To fill are the sums of the loading times per day.

Example:

created_at evse_id duration
2023-02-01 15:31:01.490 DEBDOE7161977714 15
2023-02-01 15:31:05.650 DEICTE0001940 15
2023-02-01 15:45:28.158 DEWITE0054 15
2023-02-01 15:45:29.444 DEBPEE0F5540103 15
2023-02-01 17:31:01.490 DEBDOE7161977714 15
2023-02-02 15:45:53.385 DEONEEJK5A 15
2023-02-02 15:45:58.703 DEVKWE4013 15
2023-02-02 17:45:53.385 DEONEEJK5A 15

I would like to have it like this:

evse_id 2023-02-01 2023-02-02
DEBDOE7161977714 30 0
DEICTE0001940 15 0
DEWITE0054 15 0
DEBPEE0F5540103 15 0
DEONEEJK5A 0 30
DEVKWE4013 0 15

Could you please help? 将时间序列数据转换为日期周期列(汇总求和)

Thank you & Regards, Fabi

答案1

得分: 1

你可以使用tablefunc模块中的crosstab来实现这个功能。类似这样:

SELECT * FROM crosstab(
    'SELECT evse_id, created_at::date::text, SUM(duration) as dayduration 
    FROM demo
    GROUP BY evse_id, created_at::date ORDER BY 1, 2',
    'SELECT date_trunc(''day'', dd)::date::text FROM generate_series(''2023-02-01'',''2023-02-28'', ''1 day''::interval) dd'
) AS report(evse_id text, "2023-02-01" int, "2023-02-02" int, "2023-02-03" int, "2023-02-04" int, 
    "2023-02-05" int, "2023-02-06" int, "2023-02-07" int, "2023-02-08" int, 
    "2023-02-09" int, "2023-02-10" int, "2023-02-11" int, "2023-02-12" int, 
    "2023-02-13" int, "2023-02-14" int, "2023-02-15" int, "2023-02-16" int, 
    "2023-02-17" int, "2023-02-18" int, "2023-02-19" int, "2023-02-20" int, 
    "2023-02-21" int, "2023-02-22" int, "2023-02-23" int, "2023-02-24" int, 
    "2023-02-25" int, "2023-02-26" int, "2023-02-27" int, "2023-02-28" int);

简要说明一下,这里的crosstab接受两个参数。第一个参数(source_sql)定义了源数据。这个源数据必须包含三列,第一列是行的“名称”,第二列是列名,第三列通常是聚合值。

第二个参数(category_sql)用于生成列,本例中我们使用了一个系列来生成日期列。

最后,我们需要定义输出列,这些列包括行名称和日期列。请注意,定义的列数必须与导出数据中的列数相匹配。

如果你还没有tablefunc模块,可以运行命令 create extension tablefunc; 来创建它。

请确保将demo替换为你的表名!

英文:

You can achieve this with crosstab from the tablefunc module. Something like:

SELECT * FROM crosstab(
	'SELECT evse_id, created_at::date::text, SUM(duration) as dayduration 
FROM demo
GROUP BY evse_id, created_at::date ORDER BY 1, 2',
	'SELECT date_trunc(''day'', dd)::date::text FROM generate_series(''2023-02-01'',''2023-02-28'', ''1 day''::interval) dd'
) AS report(evse_id text, "2023-02-01" int, "2023-02-02" int, "2023-02-03" int, "2023-02-04" int, 
			"2023-02-05" int, "2023-02-06" int, "2023-02-07" int, "2023-02-08" int, 
			"2023-02-09" int, "2023-02-10" int, "2023-02-11" int, "2023-02-12" int, 
			"2023-02-13" int, "2023-02-14" int, "2023-02-15" int, "2023-02-16" int, 
			"2023-02-17" int, "2023-02-18" int, "2023-02-19" int, "2023-02-20" int, 
			"2023-02-21" int, "2023-02-22" int, "2023-02-23" int, "2023-02-24" int, 
			"2023-02-25" int, "2023-02-26" int, "2023-02-27" int, "2023-02-28" int);

By way of explanation, crosstab here takes two parameters, the first (source_sql) defines the source data. This must have precisely three columns, of which first is the row "names", the second the columns and the third is (usually) the aggregated value.

The second parameter (category_sql) gives us the columns, which in this case we generate using a series.

Finally we need to define the output columns, which are the row names plus the data columns. Note the number of columns defined must match the number of columns in the exported data.

If you don't already have the tablefunc module, running the command create extension tablefunc; should create it for you.

Be sure to replace demo with your table name!

huangapple
  • 本文由 发表于 2023年3月9日 20:38:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75684740.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定