0

我有一个表,其中的列包含要转换为字符串的数组,因此我可以通过分隔符将它们拆分为多个列。

我在处理带有时区的日期数组时遇到了麻烦。

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

但这会引发错误:

“函数 array_to_string(带有时区 [] 的时间戳)不存在”

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

4

1 回答 1

0

我找到了一种方法来实现这一点,方法是从 timestampz 转换为 text 然后再像这样返回:

(nullif(split_part(string_agg(distinct event_date::text,'; '),'; ',1),'')::date) as date1,
于 2019-02-15T03:42:31.897 回答