首页 > 解决方案 > 如何复制累积字段

问题描述

我正在尝试从我在 Datastudio 中准备的 XLS 中复制数据,

在此处输入图像描述

维度为 YYYYMM,输出为 MonthlyRate。基本上 CumulativeResponse/CumulativeMeetings = MonthlyRate

我应该如何准备 Datastudio 内的计算字段和图形?

我当前使用的计算字段是 Sum(Response) / Count(Meetings) 并使用维度 YYYYMM(YearMonth)。场上的平均水平,但最终导致数字略有偏差。例如,对于 201809,0.45 变为 0.47。

标签: sqlexcelgoogle-bigquerygoogle-data-studio

解决方案


以下示例适用于 BigQuery 标准 SQL

#standardSQL
WITH `project.dataset.table` AS (
  SELECT '201712' yyyymm, 4580 SumResponse, 6741 CountMeetings UNION ALL
  SELECT '201801', 3574, 6926 UNION ALL
  SELECT '201802', 2020, 6433 UNION ALL
  SELECT '201803', 1895, 6635 UNION ALL
  SELECT '201804', 2174, 6163 UNION ALL
  SELECT '201805', 3058, 7697 UNION ALL
  SELECT '201806', 3313, 7838 UNION ALL
  SELECT '201807', 4043, 8586 UNION ALL
  SELECT '201808', 5053, 9355 UNION ALL
  SELECT '201809', 1122, 1300 
)
SELECT 
  yyyymm, 
  SumResponse, 
  SUM(SumResponse) OVER(ORDER BY yyyymm) CumulativeResponse , 
  CountMeetings,
  SUM(CountMeetings) OVER(ORDER BY yyyymm) CumulativeMeetings,
  SUM(SumResponse) OVER(ORDER BY yyyymm)/SUM(CountMeetings) OVER(ORDER BY yyyymm) MonthlyRate 
FROM `project.dataset.table`
ORDER BY yyyymm   

结果:

Row yyyymm  SumResponse CumulativeResponse  CountMeetings   CumulativeMeetings  MonthlyRate  
1   201712  4580        4580                6741            6741                0.6794244177421748   
2   201801  3574        8154                6926            13667               0.596619594644033    
3   201802  2020        10174               6433            20100               0.5061691542288557   
4   201803  1895        12069               6635            26735               0.45143070880867775  
5   201804  2174        14243               6163            32898               0.4329442519302085   
6   201805  3058        17301               7697            40595               0.4261854908239931   
7   201806  3313        20614               7838            48433               0.4256188962071315   
8   201807  4043        24657               8586            57019               0.4324348024342763   
9   201808  5053        29710               9355            66374               0.4476150299816193   
10  201809  1122        30832               1300            67674               0.4555959452670154   

推荐阅读