从 SQL 中选择 JSON 并按 ID 连接元素
Posted
技术标签:
【中文标题】从 SQL 中选择 JSON 并按 ID 连接元素【英文标题】:Select JSON from SQL and JOIN elements by IDs 【发布时间】:2018-07-28 15:30:06 【问题描述】:问题:如何为每个问题/答案对返回一行?我觉得我几乎有过它,但是答案中的嵌套 ID 让我失望了,因为我正在尝试 Regex,但我想有一种更纯粹的 JSON 方式可以在没有任何 Regex 的情况下做到这一点,对吧?
这就是我要找的东西:
i.e.
--------------------------------------------------------------------------------
Question Answer
What do you think? Love it. So easy to use.
Which sales channels do you use? Facebook / Instagram
Scale of 1 to 10 how much do you like this? 7
这是数据:
# setup our data
WITH survey_results -- table name
AS
(
SELECT '''
"fields": [
"id": "ycq4WbK9EdNF",
"title": "What do you think?",
"type": "long_text"
,
"id": "uPLaLxMKZ9lm",
"title": "Scale of 1 to 10 how much do you like this?",
"type": "opinion_scale"
,
"choices": [
"id": "k4227hvYbTaR",
"label": "eBay"
,
"id": "zjWplAMROVuP",
"label": "Etsy"
],
"id": "f6ois7aPxVz4",
"title": "Which sales channels do you use?",
"type": "multiple_choice"
],
"id": "k56nsh",
"title": "Amazon Survey"
'''
AS questions, --- column name
'''
[
"field":
"id": "ycq4WbK9EdNF",
"type": "long_text"
,
"text": "Love it. So easy to use.",
"type": "text"
,
"field":
"id": "uPLaLxMKZ9lm",
"type": "opinion_scale"
,
"number": 7,
"type": "number"
,
"choice":
"label": "Facebook / Instagram"
,
"field":
"id": "f6ois7aPxVz4",
"type": "multiple_choice"
,
"type": "choice"
]
'''
AS answers --- column name
)
# run the query
SELECT * from survey_results
【问题讨论】:
【参考方案1】:以下是 BigQuery 标准 SQL
#standardSQL
CREATE TEMPORARY FUNCTION CUSTOM_EXTRACT_QUESTIONS(json STRING)
RETURNS ARRAY<STRUCT<id STRING, question STRING>>
LANGUAGE js AS """
try var parsed = JSON.parse(json).fields; var result = [];
for (i = 0; i < parsed.length; i++)
e = []; e.id = parsed[i].id; e.question = parsed[i].title; result.push(e);
; return result; catch (e) return null
""";
CREATE TEMPORARY FUNCTION CUSTOM_EXTRACT_ANSWERS(json STRING)
RETURNS ARRAY<STRUCT<id STRING, answer STRING>>
LANGUAGE js AS """
try var parsed = JSON.parse(json);
var result = []; for (i = 0; i < parsed.length; i++)
e = []; e.id = parsed[i].field.id;
e.answer = parsed[i].text || parsed[i].number || parsed[i].choice.label; result.push(e);
; return result; catch (e) return null
""";
SELECT q.id, question, answer
FROM survey_results,
UNNEST(CUSTOM_EXTRACT_QUESTIONS(questions)) q,
UNNEST(CUSTOM_EXTRACT_ANSWERS(answers)) a
WHERE q.id = a.id
如果应用于您问题中的虚拟数据 - 结果是(测试)
Row id question answer
1 ycq4WbK9EdNF What do you think? Love it. So easy to use.
2 uPLaLxMKZ9lm Scale of 1 to 10 how much do you like this? 7
3 f6ois7aPxVz4 Which sales channels do you use? Facebook / Instagram
【讨论】:
另外,相当复杂。如果我们取消选中标准 SQL 会更简单吗? 遇到了一个拦截器:需要在 Data Studio 中公开它,所以我尝试将此查询保存为视图。但是视图不支持临时功能。你会建议我如何解决这个问题?我还尝试在数据工作室创建自定义查询,但看起来其他人遇到了与我相同的错误:en.advertisercommunity.com/t5/Data-Studio/… 如果您要发布具有新要求的新问题,以便能够保存为视图等 - 我很可能会回答。但您还需要重新接受上述答案 - 因为它完全回答了您最初的问题:o)以上是关于从 SQL 中选择 JSON 并按 ID 连接元素的主要内容,如果未能解决你的问题,请参考以下文章
略微复杂的sql逻辑(从数据库逆序查找有限条记录(limit))并按相反顺序输出