zoukankan      html  css  js  c++  java
  • Oracle分析函数-first_value()和last_value()

    first_value()和last_value()字面意思已经很直观了,取首尾记录值。
    例:查询部门最早发生销售记录日期和最近发生的销售记录日期

    select
           dept_id
          ,sale_date
          ,goods_type
          ,sale_cnt
          ,first_value(sale_date) over (partition by dept_id order by sale_date) first_value
          ,last_value(sale_date) over (partition by dept_id order by sale_date desc) last_value
    from criss_sales;

    看结果first_value()很直观,不用多解释
    但是,last_value()值,部门D01不是应该为2014/6/12,部门D02不是应该为2014/5/2吗?为什么会每条记录都不一样?
    可以这样去理解:last_value()默认统计范围是 rows between unbounded preceding and current row
    验证一下:

    select
           dept_id
          ,sale_date
          ,goods_type
          ,sale_cnt
          ,first_value(sale_date) over (partition by dept_id order by sale_date) first_value
          ,last_value(sale_date) over (partition by dept_id order by sale_date desc) last_value
          ,last_value(sale_date) over (partition by dept_id order by sale_date rows between unbounded preceding and unbounded following) last_value_all
    from criss_sales;

    全统计的情况下得到的last_value()值,部门D01为2014/6/12,部门D02为2014/5/2。

  • 相关阅读:
    hdu1240 bfs 水题
    hdu 2102 BFS
    gym 101081E Polish Fortress 几何
    Gym 101081K Pope's work dp
    hdu 6188 贪心
    hdu 6186 水
    Codeforces Round #430 (Div. 2) A B 水 C dfs,思维 D trie,二进制
    hdu6152 拉姆齐定理
    hdu6165 缩点,dfs
    hdu6153 扩展KMP
  • 原文地址:https://www.cnblogs.com/sooner/p/7727942.html
Copyright © 2011-2022 走看看