繁体   English   中英

将多个字段合并为一个

[英]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                |
+------+------------------------------------------------------------+--------------------------+

你会发现,不仅是每一个质疑其自己的行,但有重复的question通过行id在不同的答案response 可能具有挑战性的是,ID对一个问题给出的响应数量之间不一致。 5678提供了三个答案, What 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    

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM