N
N
Neuro2019-10-01 18:06:38
PostgreSQL
Neuro, 2019-10-01 18:06:38

How to solve subquery must return only one column error?

Good afternoon.
As a result of the sql query, I want to get an object with a test field that will be an array and will contain various objects.

SELECT  sensor_instances.*,
    row_to_json((SELECT d
      FROM  (SELECT groups.*) d
    )) AS group,
    row_to_json((SELECT a
      FROM (SELECT addresses.*) a
    )) AS currentAddress,
    ARRAY(
      SELECT sensor_parameters.*
      FROM sensor_parameters
      WHERE sensor_parameters.sensortype IN (sensor_instances.sensortype)
    ) AS test
FROM sensor_instances
LEFT JOIN groups ON groups.id = sensor_instances.groupid 
LEFT JOIN addresses ON sensor_instances.addressid = addresses.id
WHERE sensor_instances.id IN (89)

But as a result, the request returns an error
Query 1 ERROR: ERROR:  subquery must return only one column
LINE 8:   ARRAY(

Maybe someone knows how to solve it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MrDywar Pichugin, 2019-10-01
@Dywar

What we have working with SQL:
SQL always returns a table in response.
The table consists of rows and columns.
One column can only have one value.
Total:
1) In order for us to get what we need, we need to glue the test array into one column, we can use the CONCAT function.
2) If we do not want to concatenate, then for each test record we output a duplicate of other data (group, currentAddress) via JOIN.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question