如何使用 array_agg 更正此 sql 查询中的语法错误
How can I correct this syntax error in this sql query with array_agg
我试图在下面的查询的第 35 行和第 43 行中包含 order by
。我想按 order
列升序排列选项和字段模型。但我收到语法错误:
syntax error at or near "AS"
LINE 42: )) AS "fields"
我正在使用 postgresql。完整代码如下:
WITH qs AS (
SELECT
"issQuestion".*,
array_agg(jsonb_build_object(
'id', "responses"."id",
'questionId', "responses"."questionId",
'title', "responses"."title",
'createdAt', "responses"."createdAt",
'updatedAt', "responses"."updatedAt"
)) AS "responses"
FROM question AS "question"
LEFT OUTER JOIN "question_response" AS "responses" ON "question"."id" = "responses"."questionId" AND "responses"."supervisionId" = 59
WHERE "question".id = 135
GROUP BY "question".id, "question".title, "question"."createdAt", "question"."updatedAt"
), qs_op AS (
SELECT
qs.*,
array_agg(jsonb_build_object(
'id', "options"."id",
'text', "options"."text",
'score', "options"."score",
'order', "options"."order"
)) AS "options"
order by 'order' ASC,
array_agg(jsonb_build_object(
'id', "fields"."id",
'name', "fields"."name",
'label', "fields"."label",
'order', "fields"."order",
'isNumeric', "fields"."isNumeric"
)) AS "fields"
order by 'order' ASC,
FROM qs
LEFT OUTER JOIN "question_option" AS "options" ON qs.id = "options"."questionId"
LEFT OUTER JOIN "question_field" AS "fields" ON qs.id = "fields"."questionId"
GROUP BY qs.id, qs.title, qs."createdAt", qs."updatedAt", qs."responses"
), qs_op_2 AS (
SELECT
qs_op.*,
array_agg(jsonb_build_object(
'id', "ft"."id",
'name', "ft"."name"
)) AS "associatedFacilityTypes"
FROM qs_op
LEFT OUTER JOIN ( "question_facility_type" AS "iqf" INNER JOIN "fac_type" AS "ft" ON "ft"."id" = "iqf"."facilityTypeId") ON qs_op.id = "iqf"."questionId"
GROUP BY qs_op.id, qs_op.title, qs_op."createdAt", qs_op."updatedAt", qs_op."responses", qs_op."options", qs_op."fields"
)
SELECT * FROM qs_op_2
ORDER BY qs_op_2.id;
看起来 ORDER BY
对应于 ARRAY_AGG()
函数内部使用的数据。在这种情况下,它必须放在函数内部(完整语法为 described here):
array_agg(jsonb_build_object(
'id', "options"."id",
'text', "options"."text",
'score', "options"."score",
'order', "options"."order"
) ORDER BY "options"."order" ASC) AS "options"
我试图在下面的查询的第 35 行和第 43 行中包含 order by
。我想按 order
列升序排列选项和字段模型。但我收到语法错误:
syntax error at or near "AS"
LINE 42: )) AS "fields"
我正在使用 postgresql。完整代码如下:
WITH qs AS (
SELECT
"issQuestion".*,
array_agg(jsonb_build_object(
'id', "responses"."id",
'questionId', "responses"."questionId",
'title', "responses"."title",
'createdAt', "responses"."createdAt",
'updatedAt', "responses"."updatedAt"
)) AS "responses"
FROM question AS "question"
LEFT OUTER JOIN "question_response" AS "responses" ON "question"."id" = "responses"."questionId" AND "responses"."supervisionId" = 59
WHERE "question".id = 135
GROUP BY "question".id, "question".title, "question"."createdAt", "question"."updatedAt"
), qs_op AS (
SELECT
qs.*,
array_agg(jsonb_build_object(
'id', "options"."id",
'text', "options"."text",
'score', "options"."score",
'order', "options"."order"
)) AS "options"
order by 'order' ASC,
array_agg(jsonb_build_object(
'id', "fields"."id",
'name', "fields"."name",
'label', "fields"."label",
'order', "fields"."order",
'isNumeric', "fields"."isNumeric"
)) AS "fields"
order by 'order' ASC,
FROM qs
LEFT OUTER JOIN "question_option" AS "options" ON qs.id = "options"."questionId"
LEFT OUTER JOIN "question_field" AS "fields" ON qs.id = "fields"."questionId"
GROUP BY qs.id, qs.title, qs."createdAt", qs."updatedAt", qs."responses"
), qs_op_2 AS (
SELECT
qs_op.*,
array_agg(jsonb_build_object(
'id', "ft"."id",
'name', "ft"."name"
)) AS "associatedFacilityTypes"
FROM qs_op
LEFT OUTER JOIN ( "question_facility_type" AS "iqf" INNER JOIN "fac_type" AS "ft" ON "ft"."id" = "iqf"."facilityTypeId") ON qs_op.id = "iqf"."questionId"
GROUP BY qs_op.id, qs_op.title, qs_op."createdAt", qs_op."updatedAt", qs_op."responses", qs_op."options", qs_op."fields"
)
SELECT * FROM qs_op_2
ORDER BY qs_op_2.id;
看起来 ORDER BY
对应于 ARRAY_AGG()
函数内部使用的数据。在这种情况下,它必须放在函数内部(完整语法为 described here):
array_agg(jsonb_build_object(
'id', "options"."id",
'text', "options"."text",
'score', "options"."score",
'order', "options"."order"
) ORDER BY "options"."order" ASC) AS "options"