我目前拥有与此类似的数据:
+------+------------------------------------------------------------+--------------------------+
| id | question | response |
+------+------------------------------------------------------------+--------------------------+
| 1234 | What did you enjoy the most about your experience with us? | Delivery |
| 1234 | What did you enjoy the most about your experience with us? | Customer Service |
| 1234 | What about our Customer Service could we improve? | Response Time |
| 1234 | What about our Customer Service could we improve? | Less Email |
| 1234 | What other products would you like to see us make? | Table |
| 5678 | What about our Customer Service could we improve? | Response Time |
| 5678 | What about our Customer Service could we improve? | Site Navigation |
| 5678 | What other products would you like to see us make? | Bookshelf |
| 5678 | What other products would you like to see us make? | Table |
| 5678 | What other products would you like to see us make? | Chairs |
| 9999 | What did you enjoy the most about your experience with us? | Customer Service |
| 9999 | What did you enjoy the most about your experience with us? | Ease of Assembly |
| 9999 | What did you enjoy the most about your experience with us? | Pricing |
| 9999 | What about our delivery could we improve? | Shipping Time |
| 9999 | What about our delivery could we improve? | Custom Delivery |
| 9999 | What other products would you like to see us make? | Bookshelf |
+------+------------------------------------------------------------+--------------------------+
您会注意到,不仅每个问题都有自己的行,而且question
重复id
行,在response
中有不同的答案。可能具有挑战性的是,ID对一个问题给出的响应数量之间不一致。 5678
对What other products would you like to see us make?
给出了三个答案,而9999
仅回答了一个。我不确定这是否相关,但是ID可以为问题提供答案的数量永远不会超过四个。答案是从列表中预先设置的。
我想以一种在question
和response
之间创建1:1答案的方式格式化数据,例如:
+------+------------------------------------------------------------+---------------------------------------------+
| id | question | response |
+------+------------------------------------------------------------+---------------------------------------------+
| 1234 | What did you enjoy the most about your experience with us? | Delivery, Customer Service |
| 1234 | What about our Customer Service could we improve? | Response Time, Less Email |
| 1234 | What other products would you like to see us make? | Table |
| 5678 | What about our Customer Service could we improve? | Response Time, Site Navigation |
| 5678 | What other products would you like to see us make? | Bookshelf, Table, Chairs |
| 9999 | What did you enjoy the most about your experience with us? | Customer Service, Ease of Assembly, Pricing |
| 9999 | What about our delivery could we improve? | Shipping Time, Custom Delivery |
| 9999 | What other products would you like to see us make? | Bookshelf |
+------+------------------------------------------------------------+---------------------------------------------+
将响应用逗号分隔会很有帮助,但是我不确定是否必须通过分区上某种形式的隐藏来完成,或者是否有某种内置函数可以做到这一点。
答案 0 :(得分:3)
以下是用于BigQuery标准SQL
#standardSQL
SELECT id, question, STRING_AGG(response, ', ') response
FROM `project.dataset.table`
GROUP BY id, question
您可以使用问题中的示例数据来测试,玩游戏,如下例所示
#standardSQL
WITH `project.dataset.table` AS (
SELECT 1234 id, 'What did you enjoy the most about your experience with us?' question, 'Delivery' response UNION ALL
SELECT 1234, 'What did you enjoy the most about your experience with us?', 'Customer Service' UNION ALL
SELECT 1234, 'What about our Customer Service could we improve?', 'Response Time' UNION ALL
SELECT 1234, 'What about our Customer Service could we improve?', 'Less Email' UNION ALL
SELECT 1234, 'What other products would you like to see us make?', 'Table' UNION ALL
SELECT 5678, 'What about our Customer Service could we improve?', 'Response Time' UNION ALL
SELECT 5678, 'What about our Customer Service could we improve?', 'Site Navigation' UNION ALL
SELECT 5678, 'What other products would you like to see us make?', 'Bookshelf' UNION ALL
SELECT 5678, 'What other products would you like to see us make?', 'Table' UNION ALL
SELECT 5678, 'What other products would you like to see us make?', 'Chairs' UNION ALL
SELECT 9999, 'What did you enjoy the most about your experience with us?', 'Customer Service' UNION ALL
SELECT 9999, 'What did you enjoy the most about your experience with us?', 'Ease of Assembly' UNION ALL
SELECT 9999, 'What did you enjoy the most about your experience with us?', 'Pricing' UNION ALL
SELECT 9999, 'What about our delivery could we improve?', 'Shipping Time' UNION ALL
SELECT 9999, 'What about our delivery could we improve?', 'Custom Delivery' UNION ALL
SELECT 9999, 'What other products would you like to see us make?', 'Bookshelf'
)
SELECT id, question, STRING_AGG(response, ', ') response
FROM `project.dataset.table`
GROUP BY id, question
-- ORDER BY id, question
有结果
Row id question response
1 1234 What about our Customer Service could we improve? Response Time, Less Email
2 1234 What did you enjoy the most about your experience with us? Delivery, Customer Service
3 1234 What other products would you like to see us make? Table
4 5678 What about our Customer Service could we improve? Response Time, Site Navigation
5 5678 What other products would you like to see us make? Bookshelf, Table, Chairs
6 9999 What about our delivery could we improve? Shipping Time, Custom Delivery
7 9999 What did you enjoy the most about your experience with us? Customer Service, Ease of Assembly, Pricing
8 9999 What other products would you like to see us make? Bookshelf