How to generate JSON array from multiple rows, then return with values of another table

Exitialis

I am trying to build a query which combines rows of one table into a JSON array, I then want that array to be part of the return.

I know how to do a simple query like

SELECT *
FROM public.template 
WHERE id=1

And I have worked out how to produce the JSON array that I want

SELECT array_to_json(array_agg(to_json(fields)))
FROM (
    SELECT id, name, format, data
    FROM public.field
    WHERE template_id = 1
) fields

However, I cannot work out how to combine the two, so that the result is a number of fields from public.template with the output of the second query being one of the returned fields.

I am using PostGreSQL 9.6.6

Edit, as requested more information, a definition of field and template tables and a sample of each queries output.

Currently, I have a JSONB row on the template table which I am using to store an array of fields, but I want to move fields to their own table so that I can more easily enforce a schema on them.

Template table contains:

  • id
  • name
  • data
  • organisation_id

But I would like to remove data and replace it with the field table which contains:

  • id
  • name
  • format
  • data
  • template_id

At the moment the output of the first query is:

  {
    "id": 1,
    "name": "Test Template",
    "data": [
      {
        "id": "1",
        "data": null,
        "name": "Assigned User",
        "format": "String"
      },
      {
        "id": "2",
        "data": null,
        "name": "Office",
        "format": "String"
      },
      {
        "id": "3",
        "data": null,
        "name": "Department",
        "format": "String"
      }
    ],
    "id_organisation": 1
  }

This output is what I would like to recreate using one query and both tables. The second query outputs this, but I do not know how to merge it into a single query:

[{
    "id": 1,
    "name": "Assigned User",
    "format": "String",
    "data": null
},{
    "id": 2,
    "name": "Office",
    "format": "String",
    "data": null
},{
    "id": 3,
    "name": "Department",
    "format": "String",
    "data": null
}]
Exitialis

Sorry for poorly phrasing what I was trying to achieve, after hours of Googling I have worked it out and it was a lot more simple than I thought in my ignorance.

SELECT id, name, data
FROM public.template, (
    SELECT array_to_json(array_agg(to_json(fields)))
        FROM (
            SELECT id, name, format, data
            FROM public.field
            WHERE template_id = 1
        ) fields
) as data
WHERE id = 1

I wanted the result of the subquery to be a column in the ouput rather than compiling the entire output table as a JSON.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to extract multiple rows from a table based on values from multiple columns from another table and then concatenate in SQL?

From Dev

How to extract values from array json column into multiple rows in Postgresql?

From Dev

How to return rows of data from a table with json?

From Dev

Postgres insert into table multiple return values from with rows as

From Dev

Update multiple rows in one table with differing values from another

From Dev

How to retrieve a row from a table and set that values to a json array and get that json array as return value

From Dev

How to Generate column heading and table values dynamically from JSON array in angularjs?

From Dev

How to generate array from multiple mysql rows with a common value

From Dev

How to generate multiple arrays containing elements from another array?

From Dev

JQuery: How to drag multiple rows from one table to another?

From Dev

JQuery: How to drag multiple rows from one table to another?

From Dev

how to insert multiple rows from array values with input

From Dev

oracle - How to insert unique values from a table to another multiple times?

From Dev

How to insert values from JSON data with multiple objects into rows in MySQL?

From Dev

Insert multiple rows from select into another table

From Dev

Field involving multiple rows from another table

From Dev

MYSQL - UPDATE multiple rows from another table

From Dev

insert multiple rows mysql from another table

From Dev

Trying to return values from another table

From Dev

Post multiple values from inputs, loop through post array, save changes to multiple table rows dotnet core

From Dev

Create multiple rows in table from array?

From Dev

How to insert multiple rows from a table to another table based on date condition (PHP-MySQL-Query)?

From Dev

Return values from multiple rows as columns with group by on multiple colum

From Dev

Returning values from a temp table if another temp table returns no rows

From Dev

Returning values from a temp table if another temp table returns no rows

From Dev

MongoDB query to return multiple values from an array

From Dev

MongoDB query to return multiple values from an array

From Dev

How to copy cell values from multiple (but not all) rows and columns from one sheet to another sheet

From Java

How to fast return and count rows from one table based on filter by two another tables

Related Related

  1. 1

    How to extract multiple rows from a table based on values from multiple columns from another table and then concatenate in SQL?

  2. 2

    How to extract values from array json column into multiple rows in Postgresql?

  3. 3

    How to return rows of data from a table with json?

  4. 4

    Postgres insert into table multiple return values from with rows as

  5. 5

    Update multiple rows in one table with differing values from another

  6. 6

    How to retrieve a row from a table and set that values to a json array and get that json array as return value

  7. 7

    How to Generate column heading and table values dynamically from JSON array in angularjs?

  8. 8

    How to generate array from multiple mysql rows with a common value

  9. 9

    How to generate multiple arrays containing elements from another array?

  10. 10

    JQuery: How to drag multiple rows from one table to another?

  11. 11

    JQuery: How to drag multiple rows from one table to another?

  12. 12

    how to insert multiple rows from array values with input

  13. 13

    oracle - How to insert unique values from a table to another multiple times?

  14. 14

    How to insert values from JSON data with multiple objects into rows in MySQL?

  15. 15

    Insert multiple rows from select into another table

  16. 16

    Field involving multiple rows from another table

  17. 17

    MYSQL - UPDATE multiple rows from another table

  18. 18

    insert multiple rows mysql from another table

  19. 19

    Trying to return values from another table

  20. 20

    Post multiple values from inputs, loop through post array, save changes to multiple table rows dotnet core

  21. 21

    Create multiple rows in table from array?

  22. 22

    How to insert multiple rows from a table to another table based on date condition (PHP-MySQL-Query)?

  23. 23

    Return values from multiple rows as columns with group by on multiple colum

  24. 24

    Returning values from a temp table if another temp table returns no rows

  25. 25

    Returning values from a temp table if another temp table returns no rows

  26. 26

    MongoDB query to return multiple values from an array

  27. 27

    MongoDB query to return multiple values from an array

  28. 28

    How to copy cell values from multiple (but not all) rows and columns from one sheet to another sheet

  29. 29

    How to fast return and count rows from one table based on filter by two another tables

HotTag

Archive