我需要获取所有度量标准列的MoM和YoY值。 MoM列应具有上个月的值,同样的情况与YoY相同。 我可以用一个维度来做,但因为有很多维度所以得到错误的信息。
下面是我为所有维度添加过滤器时正在运行的查询: -
SELECT section,region,country,type,device,month,page_views,
LAG(page_views, 1) OVER (ORDER BY month) as MoM,
LAG(page_views, 12) OVER (ORDER BY month) as YoY
FROM table_name
Where region = 'RUCIS'
and Country = 'Russia'
and section ='METRICS'
and type_visits = 'All Visits'
and device = 'Non Mobile Devices'
哪个工作正常但如果我删除where条件,因为有许多地区和许多国家,在这种情况下它不起作用。
我怎么能为所有维度做到这一点?
答案 0 :(得分:1)
如果代码使用where
子句,但您希望它在没有代码的情况下正常工作,那么您可以使用partition by
:
SELECT section, region, country, type, device, month, page_views,
LAG(page_views, 1) OVER (PARTITION BY section, region, country, type_visits, device
ORDER BY month) as MoM,
LAG(page_views, 12) OVER (PARTITION BY section, region, country, type_visits, device
ORDER BY month) as YoY
FROM table_name
WHERE region = 'RUCIS' and
Country = 'Russia' and
section ='METRICS' and
type_visits = 'All Visits' and
device = 'Non Mobile Devices';
您现在应该可以删除WHERE
条款或根据您的需要进行调整。