Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从jsonb_数组_元素或jsonb_到_记录集获取的行_编号_Json_Postgresql_Jsonb - Fatal编程技术网

从jsonb_数组_元素或jsonb_到_记录集获取的行_编号

从jsonb_数组_元素或jsonb_到_记录集获取的行_编号,json,postgresql,jsonb,Json,Postgresql,Jsonb,我在jsonb数组上进行交叉连接,希望得到数组元素的行数(或其等价物)。查看行数文档,我发现除了“分区依据”之外,我还需要执行“排序依据”,但实际上我没有可以使用的排序标准——数组只是有一个设置的顺序,我需要将索引与其余数据一起检索到数组中 客户机表将有如下条目 { "id": "cj49q33oa000", "email": { "address": "", "after": "2016-06-28T12:28:58.016Z", "error": "Et co

我在jsonb数组上进行交叉连接,希望得到数组元素的行数(或其等价物)。查看行数文档,我发现除了“分区依据”之外,我还需要执行“排序依据”,但实际上我没有可以使用的排序标准——数组只是有一个设置的顺序,我需要将索引与其余数据一起检索到数组中

客户机表将有如下条目

{
  "id": "cj49q33oa000",
  "email": {
    "address": "",
    "after": "2016-06-28T12:28:58.016Z",
    "error": "Et corporis sed."
  },
  "name": "Arnold Schinner",
  "birthdate": "2016-07-29T05:09:33.693Z",
  "status": "paused",
  "sex": "f",
  "waist": [
    {
      "completed": "2017-06-23T10:37:37.500Z"
    },
    {
      "planned": "2017-06-23T10:37:37.500Z"
    },
    {
      "planned": "2017-06-23T10:37:37.500Z"
    },
    {
      "planned": "2017-06-23T10:37:37.500Z"
    }
  ]
}
我会像这样运行一个查询

SELECT client->>'id' AS id, waist.planned
FROM clients
CROSS JOIN LATERAL JSONB_TO_RECORDSET(client->'waist') AS waist(planned TIMESTAMP WITH TIME ZONE)
WHERE waist.planned IS NOT NULL
但是我需要获得
腰部。以某种方式将\u放置在\u数组中。

使用该功能


您可以使用syntax中的行将
json_to_记录集
jsonb_to_记录集
与有序性相结合

早在9.5版本的文档中就提到

来自(函数_调用[,…])[具有顺序性][AS]表_别名[(列_别名[,…])]]的行

因此,这是可行的(在12个版本上进行了测试,但至少在>=9.5的所有版本上都应该有效)

结果:

id|name    |bool |ordinality|
--|--------|-----|----------|
 1|somename|true |         1|
 2|        |false|         2|
不过,我确实想知道这是否比在()上添加一个
row\u number()更安全…

WITH my_json AS (
    SELECT '[{"id":1, "name":"somename", "bool":true},{"id":2, "name":null, "bool":false}]'::json jsn
)
SELECT jsn_with_ordinality.*
FROM my_json,
    ROWS FROM (json_to_recordset(jsn) AS (id int, name TEXT, bool boolean)) WITH ORDINALITY jsn_with_ordinality;
id|name    |bool |ordinality|
--|--------|-----|----------|
 1|somename|true |         1|
 2|        |false|         2|