1.语法
function1(argument1,argument......argumentN) --(function处理的数据源(table)中,包含所有表的字段,不受partition-by的影响,与group by 不同)
over([partition-by-clause] [order-by-clause] [windowing-clause])
2.语法解释
argument --处理的字段
partition-by --分区子句,限制function处理的上下边界
order by -- 对分区中的数据进行排序 ,当windowing-clause 为 rows between unbounded preceding and current now(当前列),影响最大
windowing-clause --是在partition-by的条件下进行限制,例如rows between unbounded preceding and unbounded following,指的是partition-by的条件下全部数据,不能跨分区
3.例子
3.1 求product, country, region,year分区下每年的销售额
select year, week,sale,
sum (sale) over(
partition by product, country, region,year
order by week --行排序没有作用
rows between unbounded preceding and unbounded following --跨整个分区
) running_sum_ytd
from sales_fact
where country in ('Australia') and product ='Xtend Memory'
order by product, country,year, week
3.2 求product, country, region,year分区下到当前周的销售额
select year, week,sale,
sum (sale) over(
partition by product, country, region,year
order by week --行排序起到关键性作用
rows between unbounded preceding and current now --没有跨整个分区,在分区首列到当前列
) running_sum_ytd
from sales_fact
where country in ('Australia') and product ='Xtend Memory'
order by product, country,year, week