PostgreSQL处理jsonb中的arrays of array 类型

一、提出问题

在postgre数据库,遇到了数组内嵌套数组的jsonb存储格式,如下:

SELECT *
FROM public.keyword_trend

查询结果:PostgreSQL处理jsonb中的arrays of array 类型_第1张图片
其中,history一栏[[“2014-05-11”,70],[“2014-05-18”,69]…]中,每个数组包含两项,分别为日期和得分,我们希望查询最近得分(2019年2,3,4月平均得分)环比增长大于100%的keyword。

二、解决方法

先上代码:

with t1 as ( 
    select keyword,to_date(h->>0,'YYYY MM DD') as day,(h->>1)::integer as num
    from keyword_trend as t, jsonb_array_elements(history) as h
), t2 as(
    select keyword,extract(year from day)::integer as year,
             extract(month from day)::integer as month,
             extract(day from day)::integer as day,num
    from t1
), t3 as (
    select keyword,year,avg(num)
    from t2
    where year in (2018,2019) and month in (2,3,4)
    group by keyword,year
),t4 as (
    select t31.keyword,t31.avg as avg_2019,t32.avg as avg_2018
    from t3 as t31 cross join t3 as t32
    where t31.keyword=t32.keyword and t31.year>t32.year
)
select * 
from t4
where avg_2019-2*avg_2018 >0
  • t1:拆开history,使其竖向排列
  • t2:对日期的处理,将年月日提取出来
  • t3:筛选出计算环比需要用到的数据
  • t4:自连接得到一个三列的表:keyword,avg_2019,avg_2018
  • t5:选出环比增长大于100%的keyword

查询结果如下:
PostgreSQL处理jsonb中的arrays of array 类型_第2张图片

你可能感兴趣的:(SQL)