PostgreSQL:连接中的 json_agg 结果为嵌套数组

Aks*_*rla 3 sql postgresql join aggregate-functions

对于我的问题,我使用json_aggin join 来聚合我的结果。但这会导致嵌套数组序列。

询问:

 SELECT c.*,
       json_agg(ci.national_id) AS national_id,
       json_agg(a.address) AS address
FROM company AS c
LEFT JOIN
  (SELECT company_id,
          json_agg(json_build_object('value', national_id, 'country', country_code)) AS national_id
   FROM company_identification
   GROUP BY company_id) AS ci ON ci.company_id = c.id
LEFT JOIN
  (SELECT company_id,
          json_agg(address.*) AS address
   FROM address
   GROUP BY company_id) AS a ON a.company_id = c.id
GROUP BY c.id  
Run Code Online (Sandbox Code Playgroud)

结果:

 [
  {
    "id": "c876967d-dd8b-4068-88f4-57a438a2015c",
    "name": "S.A.1",
    "nationalId": [
      [
        {
          "value": "string",
          "country": "CL"
        }
      ]
    ],
    "address": [
      [
        {
          "id": "d1362084-e652-4900-ba51-86352b7a8ce5",
          "streetName": "First Avenue"
        },
        {
          "id": "0f785a23-6eb3-44ea-9254-34a6f47ff638",
          "streetName": "Second Avenue"
        }
      ]
    ]
  },
  {
    "id": "38557302-a6a3-4484-ae1b-27edc8c4e906",
    "name": "S.A.",
    "nationalId": [
      [
        {
          "value": "Chile",
          "country": "CL"
        },
        {
          "value": "Colombia",
          "country": "CO"
        },
        {
          "value": "Mexico",
          "country": "MX"
        }
      ]
    ],
    "address": [
      [
        {
          "id": "d1362084-e652-4900-ba51-86352b7a8ce5",
          "streetName": "First Avenue"
        },
        {
          "id": "0f785a23-6eb3-44ea-9254-34a6f47ff638",
          "streetName": "Second Avenue"
        }
      ]
    ]
  }
]
Run Code Online (Sandbox Code Playgroud)

正如您所看到的nationalIdaddressfields 包含嵌套数组。

sqlfiddle:http://www.sqlfiddle.com/#!17/9fde6/ 2

sqlfiddle 的输出:

| id |  name |                                   national_id |
|----|-------|-----------------------------------------------|
|  1 | S.A.1 | [[{"value" : "Chile", "country" : "CL "}]]    |
|  2 |  S.A. | [[{"value" : "Colombia", "country" : "CO "}]] |
Run Code Online (Sandbox Code Playgroud)

national_id 字段不应包含数组内的数组

[[ ... ]] 
Run Code Online (Sandbox Code Playgroud)

期望的输出:

| id |  name |                                   national_id |
|----|-------|-----------------------------------------------|
|  1 | S.A.1 | [{"value" : "Chile", "country" : "CL "}]      |
|  2 |  S.A. | [{"value" : "Colombia", "country" : "CO "}]   |
Run Code Online (Sandbox Code Playgroud)

rav*_*oli 5

感谢您将示例发布到 SQL Fiddle。我认为你的双数组的原因是你在外部使用JSON_AGGaround 。尝试删除它和:national_idSELECTGROUP BY

SELECT 
  "c".*,
--  JSON_AGG(ci.national_id) AS national_id
  ci.national_id
FROM "company" AS "c"
LEFT JOIN(
  SELECT "company_id",
    JSON_AGG(
      JSON_BUILD_OBJECT(
        'value', national_id, 
        'country', country_code
      )
    ) AS national_id
  FROM "company_identification"
  GROUP BY "company_id"
) AS "ci" ON "ci"."company_id" = "c"."id"
LEFT JOIN(
  SELECT "company_id",
    JSON_AGG(address.*) AS address
  FROM "address"
  GROUP BY "company_id"
) AS "a" ON "a"."company_id" = "c"."id"
-- GROUP BY "c"."id", "c"."name"
;
Run Code Online (Sandbox Code Playgroud)

JSON_AGG您已经在两个LEFT JOINsfornational_id和中执行了,address并且它们都按分组进行了分组company_id,因此不需要JSON_AGG在您的 external 中添加额外的内容SELECT

让我知道这是否有效。

http://www.sqlfiddle.com/#!17/9fde6/8