过滤父查询

时间:2019-08-02 11:16:02

标签: superset apache-superset

我正在通过Google BigQuery连接视图。我有一个视图events_with_total_users,该视图连接了一个事件表和一个用户表,因此它包含多个事件,例如“个人资料图片已更新”或“用户电子邮件已更新”,以及用户的ID,user_group和国家/地区以及用户的数量类别和国家/地区的组合(并非每个用户都有一个事件)。

+---------+----------------------------+---------+------------+-----------+
| user_id |           event            | country | user_group | user_base |
+---------+----------------------------+---------+------------+-----------+
|       1 | Profile picture change     | US      | free       |       798 |
|       7 | Profile picture change     | US      | free       |       798 |
|       2 | Profile description change | CA      | premium    |      1239 |
|       2 | Language change            | CA      | premium    |      1239 |
|       2 | Email address change       | CA      | premium    |      1239 |
|       3 | Profile picture change     | CA      | free       |      1098 |
|       4 | Email address change       | IT      | free       |      1790 |
|       5 | Language change            | DE      | premium    |       890 |
|       6 | Profile picture change     | DE      | free       |      2673 |
|       6 | Email address change       | AT      | premium    |       745 |
+---------+----------------------------+---------+------------+-----------+

我想创建一个Superset指标,该指标显示国家/地区级别的相对变化数,但能够过滤事件和user_group。

示例:

Percentage of free users that changed their profile picture in the US: 2 / 798 = 0.22%

SQL查询以可视化我在SQL Lab中基于源表events_with_total_users在国家/地区级别创建的百分比:

WITH totals_per_user_group_and_country 
AS 
  ( 
           SELECT   country, 
                    user_group, 
                    avg(total_users) total_users 
           FROM     `events_with_total_users` 
           GROUP BY 1, 
                    2), 
  totals_per_country 
AS 
  ( 
           SELECT   country, 
                    sum(total_users) total_users 
           FROM     totals_per_category_and_country 
           GROUP BY 1) 
  SELECT   a.country, 
           round(count(DISTINCT id)/avg(b.total_users)*100,2) ratio 
  FROM     `events_with_total_users` a 
  JOIN     totals_per_country b 
  USING   (country) 
  GROUP BY 1

当我在仪表板上添加此查询的条形图并基于events_with_total_users表创建过滤器框外观时,该过滤器不会影响我的条形图。我希望Superset可以过滤父源。是否可以过滤user_group和event以获得正确的百分比?

0 个答案:

没有答案