如何复制累积字段

时间:2018-09-06 02:39:00

标签: sql excel google-bigquery google-data-studio

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

enter image description here

维度为YYYYMM,输出为MonthlyRate。 基本上,累积响应/累积会议=月费率

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

我当前使用的计算字段是Sum(Response)/ Count(Meetings),使用维度YYYYMM(YearMonth)。 场上的移动平均值,但是最终导致数字稍微偏斜。 例如201809的0.45变为0.47。

1 个答案:

答案 0 :(得分:2)

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