Joh*_*ell 5 pivot pivot-table google-bigquery
我目前正在BigQuery中处理数据,然后将其导出到Excel中以完成最终的数据透视表,并希望能够使用BigQuery中的PIVOT选项创建相同的数据。
大查询中的我的数据集看起来像
Transaction_Month || ConsumerId || CUST_createdMonth
01/01/2015 || 1 || 01/01/2015
01/01/2015 || 1 || 01/01/2015
01/02/2015 || 1 || 01/01/2015
01/01/2015 || 2 || 01/01/2015
01/02/2015 || 3 || 01/02/2015
01/02/2015 || 4 || 01/02/2015
01/02/2015 || 5 || 01/02/2015
01/03/2015 || 5 || 01/02/2015
01/03/2015 || 6 || 01/03/2015
01/04/2015 || 6 || 01/03/2015
01/06/2015 || 6 || 01/03/2015
01/03/2015 || 7 || 01/03/2015
01/04/2015 || 8 || 01/04/2015
01/05/2015 || 8 || 01/04/2015
01/04/2015 || 9 || 01/04/2015
Run Code Online (Sandbox Code Playgroud)
它本质上是一个附有客户信息的订单表。
当我将此数据放入excel时,我将其添加到数据透视表中,将CUST_createdMonth添加为行,将Transaction_Month添加为列,并且该值是ConsumerID的不同Count
BigQuery是否有可能实现这种枢纽?
在 BigQuery 中没有好的方法可以做到这一点,但您可以按照以下想法进行操作
\n\n\n\n\n步骤1
\n
运行下面的查询
\n\nSELECT \'SELECT CUST_createdMonth, \' + \n GROUP_CONCAT_UNQUOTED(\n \'EXACT_COUNT_DISTINCT(IF(Transaction_Month = "\' + Transaction_Month + \'", ConsumerId, NULL)) as [m_\' + REPLACE(Transaction_Month, \'/\', \'_\') + \']\'\n ) \n + \' FROM yourTable GROUP BY CUST_createdMonth ORDER BY CUST_createdMonth\'\nFROM (\n SELECT Transaction_Month \n FROM yourTable\n GROUP BY Transaction_Month\n ORDER BY Transaction_Month\n) \nRun Code Online (Sandbox Code Playgroud)\n\n结果 - 您将得到如下所示的字符串(为了便于阅读,其格式如下)
\n\nSELECT\n CUST_createdMonth,\n EXACT_COUNT_DISTINCT(IF(Transaction_Month = "01/01/2015", ConsumerId, NULL)) AS [m_01_01_2015],\n EXACT_COUNT_DISTINCT(IF(Transaction_Month = "01/02/2015", ConsumerId, NULL)) AS [m_01_02_2015],\n EXACT_COUNT_DISTINCT(IF(Transaction_Month = "01/03/2015", ConsumerId, NULL)) AS [m_01_03_2015],\n EXACT_COUNT_DISTINCT(IF(Transaction_Month = "01/04/2015", ConsumerId, NULL)) AS [m_01_04_2015],\n EXACT_COUNT_DISTINCT(IF(Transaction_Month = "01/05/2015", ConsumerId, NULL)) AS [m_01_05_2015],\n EXACT_COUNT_DISTINCT(IF(Transaction_Month = "01/06/2015", ConsumerId, NULL)) AS [m_01_06_2015]\n FROM yourTable \nGROUP BY\n CUST_createdMonth\nORDER BY\n CUST_createdMonth\nRun Code Online (Sandbox Code Playgroud)\n\n\n\n\n第2步
\n
只需运行上面的组合查询
\n\n结果将类似于下面的 e
\n\nCUST_createdMonth m_01_01_2015 m_01_02_2015 m_01_03_2015 m_01_04_2015 m_01_05_2015 m_01_06_2015 \n01/01/2015 2 1 0 0 0 0 \n01/02/2015 0 3 1 0 0 0 \n01/03/2015 0 0 2 1 0 1 \n01/04/2015 0 0 0 2 1 0 \nRun Code Online (Sandbox Code Playgroud)\n\n\n\n\n笔记
\n
如果您有几个月的时间需要进行大量的手动工作,那么第 1 步会很有帮助。
\n在这种情况下 - 步骤 1 可帮助您生成查询
\n\n\n您可以在我的其他帖子中了解有关旋转的更多信息。
\n
如何在 BigQuery 中扩展数据透视?
\n请注意 \xe2\x80\x93 每个表的列数限制为 10K - 因此您只能使用 10K 个组织。
\n您还可以看到下面的简化示例(如果上面的示例过于复杂/冗长):
\n如何将 BigQuery/SQL 中的行转置为包含大量数据的列?
\n如何在 Google BigQuery 中为数千个类别创建虚拟变量列?
\n在 BigQuery 中透视重复字段
| 归档时间: |
|
| 查看次数: |
6433 次 |
| 最近记录: |