分析函數
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
分析函數