I am using PostgreSQL 9.5
I have two table:
A
ID | isStatusA | IsStatusB | IsStatusC
0 | true | false | true
A table has only 1 row for data i need. Also i have B table with:
ID | status | a_id
0 | A | 0
0 | C | 0
When i wrote my select with "from A inner join B on a.id = b.a_id" that i get 2 rows. I have to get only one row (or json object) with checking that is that true/false in table A and is status present in table B. To return true i have to check both conditions.
I want {A: true, B: false, C:true}
or something similar with using pivot.
2
Answers
Using PostgreSQL
ARRAY_AGG()
function along withGROUP BY
to "denormalize" theB
tablestatus
column. ThenINNER JOIN
tableA
with an ad hoc, temporary table, here namedB_agg
.Output:
The temporary table query:
…outputs:
This result is then is
INNER JOIN
‘ed with tableA
connecting columnsA.ID
andB_agg.a_id
:ON A.ID = B_agg.a_id
.The temporary table is given the alias
B_agg
for access outside the temporary table query, such as:B_agg.status_agg
.Try it here: https://onecompiler.com/postgresql/3yfyffrrg
Credit to: https://stackoverflow.com/a/6558226/2743458
Using
JSON_AGG()
andJSON_BUILD_OBJECT()
to create a JSON object and assign desired object key names:Output:
Try it here: https://onecompiler.com/postgresql/3yfyjt24r
The
A
table provides all the information necessary to produce the output{A: true, B: false, C:true}
.Can also including the
ID
column in case multiple IDs are collected in a single query:Output: