我以以下格式存储数据:
resource_name | readiops | writeiops
90832-00:29:3E 3.21 4.00
90833-00:30:3E 2.12 3.45
90834-00:31:3E 2.33 2.78
90832-00:29:3E 4.21 6.00
Run Code Online (Sandbox Code Playgroud)
我希望能够通过“-”对resource_name列进行拆分,并按拆分的第二部分对其进行分组,以便上面的数据如下所示:
array_serial | ldev | readiops | writeiops
90832 00:29:3E 3.21,4.21 4.00,6.00
90833 00:30:3E 2.12 3.45
90834 00:31:3E 2.33 2.78
Run Code Online (Sandbox Code Playgroud)
Resource_name 分为 array_serial 和 ldev 。
我尝试使用下面的查询只是为了得到一个错误。
SELECT
SUBSTRING(resource_name, 0, STRPOS(resource_name, ':')) AS array_serial,
SUBSTRING(resource_name,1, STRPOS(resource_name, ':')) AS ldev
FROM table
GROUP BY SUBSTRING(resource_name, 0, STRPOS(resource_name, ':'))
Run Code Online (Sandbox Code Playgroud)
我是 postgres 的新手。请帮忙。
使用split_part():
with my_table(resource_name, readiops, writeiops) as (
values
('90832-00:29:3E', 3.21, 4.00),
('90833-00:30:3E', 2.12, 3.45),
('90834-00:31:3E', 2.33, 2.78),
('90832-00:29:3E', 4.21, 6.00)
)
select
split_part(resource_name::text, '-', 1) as array_serial,
split_part(resource_name::text, '-', 2) as ldev,
string_agg(readiops::text, ',') as readiops,
string_agg(writeiops::text, ',') as writeiops
from my_table
group by 1, 2;
array_serial | ldev | readiops | writeiops
--------------+----------+-----------+-----------
90832 | 00:29:3E | 3.21,4.21 | 4.00,6.00
90833 | 00:30:3E | 2.12 | 3.45
90834 | 00:31:3E | 2.33 | 2.78
(3 rows)
Run Code Online (Sandbox Code Playgroud)