SQL 查询以对来自 Amazon AMS 的聚合数据进行分区
SQL Query to partition aggregate data from Amazon AMS
我正在使用 Google Big Query 来存储来自 amazon ams 的数据。每天都会将一个 csv 文件加载到数据库中,其中包含生命周期支出、点击次数和展示次数。数据看起来像这样:
date_uploaded,campaign,spend,impressions,clicks
2017-11-01,product a,100,1000,50
2017-11-01,product b,50,500,20
2017-11-02,product a,175,1600,75
2017-11-02,product b,100,1000,50
2017-11-03,product a,250,2200,110
2017-11-03,product b,150,1500,80
我想转换此数据以显示每日支出(与前一天的差异),因此最终结果如下所示:
date_uploaded,campaign,spend,impressions,clicks
2017-11-02,product a,75,600,25
2017-11-02,product b,50,500,30
2017-11-03,product a,75,600,35
2017-11-03,product b,50,500,30
有没有办法通过这种方式查询BQ来分区数据?
您可以使用 lag
执行此操作。分区中第一行的差异将是 null
并且可以使用 where
子句过滤器排除它们。这假设每个日期一行,活动。如果有多行,则将给定日期的值相加并使用 lag
.
select * from (
select date_uploaded,campaign,
spend-lag(spend) over(partition by campaign order by date_uploaded) as spend_diff,
impressions-lag(impressions) over(partition by campaign order by date_uploaded) as impressions_diff,
clicks-lag(clicks) over(partition by campaign order by date_uploaded) as clicks_diff
from tbl
) t
where spend_diff is not null and impressions_diff is not null and clicks_diff is not null
我正在使用 Google Big Query 来存储来自 amazon ams 的数据。每天都会将一个 csv 文件加载到数据库中,其中包含生命周期支出、点击次数和展示次数。数据看起来像这样:
date_uploaded,campaign,spend,impressions,clicks
2017-11-01,product a,100,1000,50
2017-11-01,product b,50,500,20
2017-11-02,product a,175,1600,75
2017-11-02,product b,100,1000,50
2017-11-03,product a,250,2200,110
2017-11-03,product b,150,1500,80
我想转换此数据以显示每日支出(与前一天的差异),因此最终结果如下所示:
date_uploaded,campaign,spend,impressions,clicks
2017-11-02,product a,75,600,25
2017-11-02,product b,50,500,30
2017-11-03,product a,75,600,35
2017-11-03,product b,50,500,30
有没有办法通过这种方式查询BQ来分区数据?
您可以使用 lag
执行此操作。分区中第一行的差异将是 null
并且可以使用 where
子句过滤器排除它们。这假设每个日期一行,活动。如果有多行,则将给定日期的值相加并使用 lag
.
select * from (
select date_uploaded,campaign,
spend-lag(spend) over(partition by campaign order by date_uploaded) as spend_diff,
impressions-lag(impressions) over(partition by campaign order by date_uploaded) as impressions_diff,
clicks-lag(clicks) over(partition by campaign order by date_uploaded) as clicks_diff
from tbl
) t
where spend_diff is not null and impressions_diff is not null and clicks_diff is not null