简体   繁体   English

数组 json 将元素拆分为列 postgres

[英]Array json split elements to colums postgres

I have this column extra which is JSONB from table called subscribers and the value for a given subscriber is:我有这个额外的列,它是来自名为订阅者的表中的JSONB ,给定订阅者的值是:

{
  "valid": "N",
  "msisdn": "23490272",
  "account_info": [
    {
      "account_id": 110000616,
      "account_cur": "NGN",
      "account_type": "C",
      "account_class": "ww"
    },
    {
      "account_id": 110000617,
      "account_cur": "NGN",
      "account_type": "S",
      "account_class": "GHCXLA"
    },
    {
      "account_id": 110000618,
      "account_cur": "EUR",
      "account_type": "S",
      "account_class": "ww"
    },
    {
      "account_id": 110000619,
      "account_cur": "USD",
      "account_type": "C",
      "account_class": "ww"
    },
    {
      "account_id": 110000620,
      "account_cur": "SAR",
      "account_type": "Y",
      "account_class": "ww"
    },
    {
      "account_id": 110000621,
      "account_cur": "NGN",
      "account_type": "Y",
      "account_class": "ww"
    },
    {
      "account_id": 110000622,
      "account_cur": "NGN",
      "account_type": "S",
      "account_class": "ww"
    },
    {
      "account_id": 110000623,
      "account_cur": "NGN",
      "account_type": "S",
      "account_class": "ww"
    },
    {
      "account_id": 110000624,
      "account_cur": "NGN",
      "account_type": "S",
      "account_class": "ww"
    },
    {
      "account_id": 110000625,
      "account_cur": "NGN",
      "account_type": "S",
      "account_class": "ww"
    },
    {
      "account_id": 110000626,
      "account_cur": "NGN",
      "account_type": "S",
      "account_class": "ww"
    },
    {
      "account_id": 110000627,
      "account_cur": "NGN",
      "account_type": "S",
      "account_class": "ww"
    },
    {
      "account_id": 110000628,
      "account_cur": "NGN",
      "account_type": "S",
      "account_class": "ww"
    },
    {
      "account_id": 110000629,
      "account_cur": "NGN",
      "account_type": "S",
      "account_class": "ww"
    },
    {
      "account_id": 110000630,
      "account_cur": "NGN",
      "account_type": "S",
      "account_class": "ww"
    }
  ],
  "preapprovalId": "1517299109619",
  "reservedAmount": 0.0,
  "preapprovalStatus": "APPROVED"
}

I want to make the elements from the account_info into columns.I have tried this:我想将 account_info 中的元素放入列中。我试过这个:

select extra #>'{account_info,0}'->>'account_id' Account,extra #>'{account_info,0}'->>'account_cur' Currency,extra #>'{account_info,0}'->>'account_type' Account_Type from subscribers s2 where id = 319;
  account  | currency | account_type
-----------+----------+--------------
 110000616 | NGN      | C

How can i have all the elements?我怎样才能拥有所有元素?

If I followed you correctly, you can use jsonb_array_elements() and a lateral join to unnest each array element to a separate row:如果我正确地跟随你,你可以使用jsonb_array_elements()和横向连接将每个数组元素取消嵌套到单独的行:

select 
    x.acc ->> 'account_id' account,
    x.acc ->> 'account_cur' currency,
    x.acc ->> 'account_type' account_type
from subscribers s
cross join lateral jsonb_array_elements(s.extra -> 'account_info') as x(acc)
where s.id = 319

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

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