简体   繁体   English

Postgres 查询嵌套 JSONB

[英]Postgres query nested JSONB

I have a JSONB column containing list of objects .>我有一个包含objects listJSONB列。>
Here's the table schema:这是表架构:

column Name | Datatype
---------------------
timestamp   | timestamp
  data      | JSONB 

Sample Data样本数据

1. 1.

timestamp : 2020-02-02 19:01:21.571429+00
data : [
  {
    "tracker_id": "5",
    "position": 1
  },
  {
    "tracker_id": "11",
    "position": 2
  },
  {
    "tracker_id": "4",
    "position": 1
  }
]

2. 2.

timestamp : 2020-02-02 19:01:23.571429+00
data : [
  {
    "tracker_id": "7",
    "position": 3
  },
  {
    "tracker_id": "4",
    "position": 2
  }
]

3. 3.

timestamp : 2020-02-02 19:02:23.571429+00
data : [
  {
    "tracker_id": "5",
    "position": 2
  },
  {
    "tracker_id": "4",
    "position": 1
  }
]

I need to find the count of the transitions of tracker_id from position: 1 to position: 2我需要找到tracker_idposition: 1position: 2的转换计数
Here, the output will be 2 , since tracker_id 4 and 5 changed their position from 1 to 2 .在这里,输出将为2 ,因为tracker_id 45将它们的position1更改为2

Note笔记
The transition should be in ascending order depending on the timestamp转换应根据timestamp按升序排列
The position change need not to be in the consecutive records. position变化不需要在连续记录中。
I'm using timescaledb extension我正在使用 timescaledb 扩展

So far I've tried querying the objects in the list od individual record, but I'm not sure how to merge the list objects of each record and query them.到目前为止,我已经尝试查询单个记录列表中的对象,但我不确定如何合并每个记录的列表对象并查询它们。

What would be the query for this?对此的查询是什么? Should I write down a stored procedure instead?我应该写下存储过程吗?

There are various functions that will help compose several database rows into a single JSON structure: row_to_json(), array_to_json(), and, array_agg().有多种函数可以帮助将多个数据库行组合成单个 JSON 结构:row_to_json()、array_to_json() 和 array_agg()。

You will then use the usual SELECT with an ORDER BY clause to get the timestamps/JSON data you want and the use above functions to create a single JSON struture.然后,您将使用带有 ORDER BY 子句的常用 SELECT 来获取所需的时间戳/JSON 数据,并使用上述函数创建单个 JSON 结构。

I don't use timescaledb extension so I would choose pure SQL solution based on unnesting json:我不使用 timescaledb 扩展,所以我会选择基于取消嵌套 json 的纯 SQL 解决方案:

with t (timestamp,data) as (values
  (timestamp '2020-02-02 19:01:21.571429+00', '[
  {
    "tracker_id": "5",
    "position": 1
  },
  {
    "tracker_id": "11",
    "position": 2
  },
  {
    "tracker_id": "4",
    "position": 1
  }
]'::jsonb),
  (timestamp '2020-02-02 19:01:23.571429+00', '[
  {
    "tracker_id": "7",
    "position": 3
  },
  {
    "tracker_id": "4",
    "position": 2
  }
]
'::jsonb),
  (timestamp '2020-02-02 19:02:23.571429+00', '[
  {
    "tracker_id": "5",
    "position": 2
  },
  {
    "tracker_id": "4",
    "position": 1
  }
]
'::jsonb)
), unnested as (
  select t.timestamp, r.tracker_id, r.position
  from t
  cross join lateral jsonb_to_recordset(t.data) AS r(tracker_id text, position int)
)
select count(*)
from unnested u1
join unnested u2
  on u1.tracker_id = u2.tracker_id
 and u1.position = 1 
 and u2.position = 2
 and u1.timestamp < u2.timestamp;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM