尝试使用
listagg()
但结果如下:
1 a,b,c
1 a,b,c
1 a,b,c
2 MMM
需要与图像bigquery类似的实现:
with data 1 as (
select
1 sk,
'a' st,
timestamp('1900-05-08 04:00:00') dt
union all
select
1 sk,
'b',
timestamp('1901-05-08 04:00:00')
union all
select
1 sk,
'c',
timestamp('1902-05-08 04:00:00')
union all
select
2 sk,
'MMM',
timestamp('1902-05-08 04:00:00')
)
select
sk,
string_agg(st, ',') over (partition by sk order by dt)
from data1
输出:
sk f0_
1 a
1 a,b
1 a,b,c
2 MMM
通过连接值与运行总计不同的地方查找上述内容
正如您可能已经读过的那样,Redshift 的 listagg() 窗口函数不支持框架子句。不过,我们可以通过绕道至 SUPER 数据类型来模拟这一点。
下面的代码创建测试数据,执行 listagg(),将这些字符串转换为超级数组,并使用 row_number() 窗口捕获分区内字符串的顺序。 Redshift 有一个 subarray() 函数,允许仅选择超级数组的部分。删除括号和引号即可产生所需的结果。
with data as (
select
1 sk,
'a' st,
'1900-05-08 04:00:00'::timestamp dt
union all
select
1 sk,
'b',
'1901-05-08 04:00:00'::timestamp
union all
select
1 sk,
'c',
'1902-05-08 04:00:00'::timestamp
union all
select
2 sk,
'MMM',
'1902-05-08 04:00:00'::timestamp
),
arrayed as (
select sk,
json_parse('["'||listagg(st, '","') within group (order by dt) over (PARTITION by sk)||'"]') as arr,
row_number() over (partition by sk order by dt)::int as rn
from data
)
select sk, translate(json_serialize(subarray(arr,0,rn)),'[]"','') as foo
from arrayed
order by sk, rn;