码迷,mamicode.com
首页 > 其他好文 > 详细

分析函数

时间:2018-01-21 13:50:59      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:ble   run   follow   数据   处理   str   between   例子   post   

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

 

分析函数

标签:ble   run   follow   数据   处理   str   between   例子   post   

原文地址:https://www.cnblogs.com/yaodidi/p/8323825.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!