将多个字段合并为一个

Consolidating multiple fields into one

我目前的数据看起来与此类似:

+------+------------------------------------------------------------+--------------------------+
|  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                |
+------+------------------------------------------------------------+--------------------------+

您会注意到,不仅每个问题都有自己的行,而且 id 重复 question 行,在 response 中有不同的答案。可能具有挑战性的是,一个 ID 对一个问题给出的回答数量不一致。 5678What other products would you like to see us make? 给出了三个答案,而 9999 只回答了一个。我不确定这是否相关,但是一个 ID 可以给出一个问题的答案数量永远不会超过四个。答案是从列表中预先设定的。

我想以在 questionresponse 之间创建 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                                   |
+------+------------------------------------------------------------+---------------------------------------------+

用逗号分隔响应会很有帮助,但我不确定是否必须通过分区上的某种形式的连接来完成,或者是否有某种内置函数可以做到这一点。

以下适用于 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