简体   繁体   English

如何在JSON数组中找到元素的索引(PostgreSQL 9.3)?

[英]How to find the index of an element within JSON array (PostgreSQL 9.3)?

I have some data in a table radio responses I am aggregating that looks like this: 我在表格radio responses有一些数据我正在聚合,看起来像这样:

SELECT question_id, arr FROM radio_responses;

  question_id |       arr       
-------------+-----------------
          73 | [1,0,0]
          73 | [1,0,0]
          73 | [0,1,0]
          73 | [0,1,0]
          73 | [0,1,0]
          73 | [0,0,1]
          73 | [0,1,0]
          73 | [0,1,0]
          73 | [0,0,1]
          73 | [0,0,1]
          73 | [1,0,0]
          74 | [1,0]
          74 | [0,1]
          74 | [1,0]
          74 | [0,1]
          74 | [1,0]
          74 | [0,1]
          77 | [0,1]
          77 | [0,1]
          77 | [0,1]

My end goal is to extract the index of the 1 from each array. 我的最终目标是从每个数组中提取1的索引。 I could not find any functions to do so with the JSON type, but I did find that I could do so with idx() if I have an int[] array. 我找不到任何使用JSON类型的函数,但是如果我有一个int[]数组,我确实发现我可以使用idx()

I have tried various solutions, but they all seem to rely on unnesting the data first, which seems unnecessary, especially since information is lost in the process (unless there's something that uses WITH ORDINALITY that I missed). 我已经尝试了各种解决方案,但它们似乎都依赖于首先取消数据,这似乎是不必要的,特别是因为信息在这个过程中丢失了(除非有一些东西使用了我错过的WITH ORDINALITY )。

I am using Postgres version 9.3. 我正在使用Postgres 9.3版。

Your question actually is not about RDBMS. 您的问题实际上与RDBMS无关。 However if you don't want to use unnesting and so on and if you stuck on the 9.3 version: 但是,如果您不想使用取消等等,并且如果您坚持使用9.3版本:

create or replace function json_array_position(a json, e int) returns int language plpythonu stable as $$
  import json;
  r = json.loads(a)
  return r.index(e)
$$;

select json_array_position('[1,2,3]'::json, 2);
select      *
          ,(select min(i) + 1
            from   generate_series(0,json_array_length(arr)-1) as gs (i)
            where  (arr->>i)::int = 1
            )                           as ind

from        radio_responses
;

+-------------+---------+-----+
| question_id | arr     | ind |
+-------------+---------+-----+
| 73          | [1,0,0] | 1   |
+-------------+---------+-----+
| 73          | [1,0,0] | 1   |
+-------------+---------+-----+
| 73          | [0,1,0] | 2   |
+-------------+---------+-----+
| 73          | [0,1,0] | 2   |
+-------------+---------+-----+
| 73          | [0,1,0] | 2   |
+-------------+---------+-----+
| 73          | [0,0,1] | 3   |
+-------------+---------+-----+
| 73          | [0,1,0] | 2   |
+-------------+---------+-----+
| 73          | [0,1,0] | 2   |
+-------------+---------+-----+
| 73          | [0,0,1] | 3   |
+-------------+---------+-----+
| 73          | [0,0,1] | 3   |
+-------------+---------+-----+
| 73          | [1,0,0] | 1   |
+-------------+---------+-----+
| 74          | [1,0]   | 1   |
+-------------+---------+-----+
| 74          | [0,1]   | 2   |
+-------------+---------+-----+
| 74          | [1,0]   | 1   |
+-------------+---------+-----+
| 74          | [0,1]   | 2   |
+-------------+---------+-----+
| 74          | [1,0]   | 1   |
+-------------+---------+-----+
| 74          | [0,1]   | 2   |
+-------------+---------+-----+
| 77          | [0,1]   | 2   |
+-------------+---------+-----+
| 77          | [0,1]   | 2   |
+-------------+---------+-----+
| 77          | [0,1]   | 2   |
+-------------+---------+-----+

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

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