大家好。
我在如何将 JSON 中的密钥收集到一行(进行连接)时遇到问题。
请求是这样的:
WITH all_values AS (
SELECT s.projectid AS projectid,
s.prompttype AS prompttype,
(s.attributes::jsonb)->>'prompt' AS prompt,
(s.attributes::jsonb)->>'description' AS description,
(s.attributes::jsonb)->>'topic' AS topic,
(s.attributes::jsonb)->>'context' AS context,
(s.attributes::jsonb)->>'use_case' AS use_case,
(s.attributes::jsonb)->>'subtitle' AS subtitle,
(s.attributes::jsonb)->>'txValues' AS txValues,
(s.attributes::jsonb)->>'flashmode' AS flashmode,
(s.attributes::jsonb)->>'skippable' AS skippable,
(s.attributes::jsonb)->>'videoMaxDuration' AS videoMaxDuration,
(s.attributes::jsonb)->>'defaultCameraFacing' AS defaultCameraFacing,
s.corpuscode AS corpuscode,
s.scriptid AS scriptid,
s.promptnum AS promptnum,
SELECT ARRAY_AGG(f)
FROM (
SELECT jsonb_object_keys(attributes::jsonb) f
FROM source_table
) AS u
FROM source_table AS s
WHERE
s.prompttype != 'input' AND (s.created > now() - interval '30 minutes' OR s.modified > now() - interval '30 minutes')
GROUP BY s.projectid, s.prompttype, prompt, description, topic, context, use_case, subtitle, txValues, flashmode, skippable, videoMaxDuration, defaultCameraFacing, corpuscode, scriptid, promptnum
)
SELECT * FROM all_values;
错误:错误:“SELECT”第 18 行或附近的语法错误:SELECT ARRAY_AGG(f)
我这样检查:
SELECT ARRAY_AGG(f)
FROM (
SELECT jsonb_object_keys(attributes::jsonb) f
FROM source_table
) AS u;
并获得输出。
在这里测试:https ://dbfiddle.uk/?rdbms=postgres_9.6&fiddle=dcf7722ab857c13c2d3c8edf172b434e
PS 理想情况下,在这个输出中,我不需要过滤所有的键,而只需要过滤其中的一部分,但这是下一个问题......
像这样编写查询是正确的: