有谁知道如何:
Unnest一个postgres数组 创建一个新数组作为"选择不同的"从那个unnested数组(所以没有元素重复)让新数组按照以前的方式排序?
实施例
我有以下3列:
order_id, channel_path, hit_timestamp_path
x a,b,b,c,d 1,2,3,4,5
y q,w,e,r,r 2,3,4,5,6
.
.
.
我想最终得到以下结论:
order_id, channel_path
x a,b,c,d
y q,w,e,r
.
.
.
问题是我这样做的方式,我最终会得到这样的结果:
order_id, channel_path
x b,a,c,d
y w,r,e,q
.
.
.
我正在尝试的初始解决方案如下:
SELECT order_id, (
SELECT array_agg(unnested_channels)
FROM (
SELECT DISTINCT unnested_channels
FROM (
SELECT unnest(channel_path) AS unnested_channels,
unnest(hit_timestamp_path) as unnested_timestamps
order by 2 asc
) AS unnested_channels_table
) AS distinct_unnested_channels_table
) AS distinct_channel_path
from dim_conversion_path;
答案 0 :(得分:1)
这样的事情:
select order_id, array_agg(x order by rn) as unique_channel_path
from (
select distinct on (order_id, a.x) order_id, a.x, a.rn
from dim_conversion_path, unnest(channel_path) with ordinality as a (x,rn)
order by 1,2,3
) t
group by order_id