0

My current schema is as follow:

  • evaluation has * sections. (1 - *)
  • evaluation_section has * evaluation_answers. (1 - *)
  • evaluation_section has * evaluation_fields. (1 - *)

I tried the following query. Which joins the sections on the evaluation_id using a subquery. This subquery will get the section answers and fields and json_agg into an array of fields and answers.

SELECT eval.*, json_agg(sections) AS "sections"
FROM "evaluation" eval
LEFT JOIN (
  SELECT section."id", section."name", section."feedback", section."weight", section."max_score", section."avg_score", section."score", section."achieved", section."evaluationId", json_agg(answers.*) AS "answers", json_agg(fields.*) AS "fields"
  FROM "evaluation_section" section
  INNER JOIN "evaluation_answer" answers ON answers."sectionId" = section."id"
  INNER JOIN "evaluation_custom_field" fields ON fields."sectionId" = section."id"
  GROUP BY section.id, section.name, section."evaluationId"
) sections ON sections."evaluationId" = eval."id"
GROUP BY eval."id"

The issue that I am facing is that I am getting a duplicate of each evaluation_answer and evaluation_fields. Here is the query outcome:

{
  "data": {
    "paginated_Evaluations": {
      "evaluations": [
        {
          "createdAt": "1679338451665",
          "id": 262,
          "sections": [
            {
              "name": "IN",
              "score": 3,
              "id": 760,
              "max_score": 3,
              "avg_score": 100,
              "achieved": true,
              "fields": [
                {
                  "id": 22,
                  "type": "SELECT"
           
                },
                {
                  "id": 22,
                  "type": "SELECT"
          
                },
                {
                  "id": 23,
                  "type": "RATING",
                  "value": [
                    "4"
                  ]
                },
                {
                  "id": 23,
                  "type": "RATING",
                  "value": [
                    "4"
                  ]
                }
              ],
              "answers": [
                {
                  "id": 981,
                  "name": "N/A",
                  "type": "na",
                  "score": 0
                },
                {
                  "id": 980,
                  "name": "Yes",
                  "type": "default",
                  "score": 3
                },
                {
                  "id": 981,
                  "name": "N/A",
                  "type": "na",
                  "score": 0
                },
                {
                  "id": 980,
                  "name": "Yes",
                  "type": "default",
                  "score": 3
                }
              ]
            },
            {
              "name": "Consultation",
              "score": 0,
              "id": 759,
              "max_score": 6,
              "avg_score": 0,
              "achieved": false,
              "fields": [
                {
                  "id": 19,
                  "type": "CHECKBOX",
                  "value": [
                    "Deposit"
                  ]
                },
                {
                  "id": 19,
                  "type": "CHECKBOX",
                  "value": [
                    "Deposit"
                  ]
                },
                {
                  "id": 20,
                  "type": "MULTISELECT"
                },
                {
                  "id": 20,
                  "type": "MULTISELECT"
                },
                {
                  "id": 21,
                  "type": "TEXTFIELD",
                  "value": [
                    "test"
                  ]
                },
                {
                  "id": 21,
                  "type": "TEXTFIELD",
                  "value": [
                    "test"
                  ]
                }
              ],
              "answers": [
                {
                  "id": 979,
                  "name": "No",
                  "type": "default",
                  "score": 0
                },
                {
                  "id": 978,
                  "name": "No",
                  "type": "default",
                  "score": 0
                },
                {
                  "id": 979,
                  "name": "No",
                  "type": "default",
                  "score": 0
                },
                {
                  "id": 978,
                  "name": "No",
                  "type": "default",
                  "score": 0
                },
                {
                  "id": 979,
                  "name": "No",
                  "type": "default",
                  "score": 0
                },
                {
                  "id": 978,
                  "name": "No",
                  "type": "default",
                  "score": 0
                }
              ]
            }
          ]
        }
      ],
      "hasMore": false
    }
  }
}

My desired outcome:

{
  "data": {
    "paginated_Evaluations": {
      "evaluations": [
        {
          "createdAt": "1679338451665",
          "id": 262,
          "sections": [
            {
              "name": "IN",
              "score": 3,
              "id": 760,
              "max_score": 3,
              "avg_score": 100,
              "achieved": true,
              "fields": [
                {
                  "id": 22,
                  "type": "SELECT"
           
                },

                {
                  "id": 23,
                  "type": "RATING",
                  "value": [
                    "4"
                  ]
                }
              ],
              "answers": [
                {
                  "id": 981,
                  "name": "N/A",
                  "type": "na",
                  "score": 0
                },
                {
                  "id": 980,
                  "name": "Yes",
                  "type": "default",
                  "score": 3
                }
              ]
            },
            {
              "name": "Consultation",
              "score": 0,
              "id": 759,
              "max_score": 6,
              "avg_score": 0,
              "achieved": false,
              "fields": [
                {
                  "id": 19,
                  "type": "CHECKBOX",
                  "value": [
                    "Deposit"
                  ]
                },
                {
                  "id": 20,
                  "type": "MULTISELECT"
                },
                {
                  "id": 21,
                  "type": "TEXTFIELD",
                  "value": [
                    "test"
                  ]
                }]
                
              ,
              "answers": [
                {
                  "id": 979,
                  "name": "No",
                  "type": "default",
                  "score": 0
                },
                {
                  "id": 978,
                  "name": "No",
                  "type": "default",
                  "score": 0
                }
              ]
            }
          ]
        }
      ],
      "hasMore": false
    }
  }
}
Charles Semaan
  • 304
  • 2
  • 13
  • 1
    try `json_agg(DISTINCT sections)` if that is not enough, you should take a look at the answers here https://stackoverflow.com/questions/30077639/distinct-on-in-an-aggregate-function-in-postgres – nbk Mar 20 '23 at 23:47

0 Answers0