0

我正在尝试聚合列“agg”中存在的数组,按产品对它们进行分组,并获取过去 13 周的所有事件。

例如:聚合的第一行值应该是这样的:聚合所有按产品分组的数组,并且只获取最近 13 周的数据。即从 2/8/2014 到 last_13(最后 13 周日期以 2/8/2014 作为参考,即 11-10-2013)

我尝试了以下方法但不起作用。

select array_cat(a.agg2) over(partition by a.PName order by a.PName ,a.week_date desc rows between 0 preceding and 13 following)
    from (
    select PName,week_date,array_agg(raw_value)  agg2,
    from vin_temp_table group by PName,week_date order by PName,week_date desc
    )a

编辑 :

询问 :

select PName,week_date,array_agg(raw_value)  agg,
        from vin_temp_table group by PName,week_date order by PName,week_date desc

结果如下数据

PName       week_date              agg
5773    2/8/2014    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
5773    2/1/2014    {0,0,0,0,0,0,0,0,0,0,0,0,0}
5773    1/25/2014   {0,0,0,0,0,0,0,0,0,0,0,0,0}
5774    1/18/2014   {0,0,0,0,0,0,0,0,0,0,0,0,0}
5775    1/11/2014   {0,0,0,0,0,0,0,0,0,0,0,0,0}
5776    12/28/2013  {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
5776    12/21/2013  {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
5776    12/14/2013  {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
5776    12/7/2013   {0,0,0,0,0,0,0,0,0,0,0,0,0}
5777    11/30/2013  {0,0,0,0,0,0,0,0,0,0,0,0,0}
5778    11/16/2013      {0,0,0,0,0,0,0,0,0,0,0,0}
5778    11/9/2013   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
5778    11/2/2013   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}

原始数据 : select * from vin_temp_table

 Pname       week_date       raw_value
    5773    10/21/2006  0
    5773    10/28/2006  0
    5773    11/18/2006  0
    5773    11/18/2006  0
    5773    1/20/2007   0
    5773    5/12/2007   0
    5773    7/21/2007   0
    5773    7/21/2007   0
    5773    8/11/2007   0
    5773    8/25/2007   0
    5773    9/1/2007    0

我什至尝试在 Pname 和 week_date 上使用 array_agg 进行分区。但是这个也没有成功,因为 Greenplum 8.2 版不支持它。

有人可以帮我吗?谢谢。

4

0 回答 0