尝试将包含timestampz数组的列拆分为Postgres中的分隔文本字符串时,“函数不存在”错误

问题描述 投票:0回答:1

我有一个表的列包含我想要转换为字符串的数组,所以我可以通过分隔符将它们分成多列。

我在使用带时区的日期数组时遇到了麻烦。

create materialized view matview1 as select
    (location) as location,
    (nullif(split_part(string_agg(distinct name,'; '),'; ',1),'')) as name1,
    (nullif(split_part(string_agg(distinct name,'; '),'; ',2),'')) as name2,
    (nullif(split_part(string_agg(distinct name,'; '),'; ',3),'')) as name3,
    (array_agg(distinct(event_date_with_timestamp))) as event_dates
    from table2 b
    group by location;

在上面的代码中,我正在创建表的物化视图,以将与某些位置相关的所有表条目合并为单个行。

如何为每个event_date条目创建其他列,就像我对名称一样(例如'name'数组中的Name1,Name2和Name3)?

我尝试将数组更改为字符串格式:

(nullif(split_part(array_to_string(array_agg(distinct(event_date_with_timestamp))),'; ',1),'')) as event_date1

但这会引发错误:

“function array_to_string(timestamp with time zone [])不存在”

并且转换为不同的数据类型总是会产生错误,说我无法从类型timestampz转换为其他任何内容。

postgresql timestamp-with-timezone array-agg
1个回答
0
投票

我找到了一种方法来实现这一点,从timestampz转换为文本然后再返回如下:

(nullif(split_part(string_agg(distinct event_date::text,'; '),'; ',1),'')::date) as date1,
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.