日期:2014-05-16  浏览次数:20518 次

ORACLE分析函数(5)---其他

1.除了使用数字来指定窗口范围,我们还可以使用日期类型,如:


2.lead和lag,返回当前窗口中与当前记录距离为n的记录。lag为向前取记录,lead为向后取记录

SELECT prod_id,
  lag(prod_list_price,1) over(order by prod_id) pre_1,
  lag(prod_list_price,2) over(order by prod_id) pre_2,
  prod_list_price,
  lead(prod_list_price,1) over(order by prod_id) lead_1,
  lead(prod_list_price,2) over(order by prod_id) lead_2
FROM products;
   PROD_ID      PRE_1      PRE_2 PROD_LIST_PRICE     LEAD_1     LEAD_2
---------- ---------- ---------- --------------- ---------- ----------
        13                                899.99     999.99     999.99
        14     899.99                     999.99     999.99     299.99
        15     999.99     899.99          999.99     299.99    1099.99
        16     999.99     999.99          299.99    1099.99    1299.99
        17     299.99     999.99         1099.99    1299.99      55.99
        18    1099.99     299.99         1299.99      55.99     599.99
        19    1299.99    1099.99           55.99     599.99     899.99
        20      55.99    1299.99          599.99     899.99      24.99
        21     599.99      55.99          899.99      24.99      21.99
        22     899.99     599.99           24.99      21.99      45.99
        23      24.99     899.99           21.99      45.99     112.99
        24      21.99      24.99           45.99     112.99     149.99
        25      45.99      21.99          112.99     149.99      44.99
        26     112.99      45.99          149.99      44.99     199.99
        27     149.99     112.99           44.99     199.99     499.99
        28      44.99     149.99          199.99     499.99       9.99
        29     199.99      44.99          499.99       9.99       8.99
        30     499.99     199.99            9.99       8.99      67.99
        31       9.99     499.99            8.99      67.99      44.99
        32       8.99       9.99           67.99      44.99      39.99
        33      67.99       8.99           44.99      39.99      49.99
        34      44.99      67.99           39.99      49.99      44.99
        35      39.99      44.99           49.99      44.99      54.99
        36      49.99      39.99           44.99      54.99      29.99

3.frist_value  last_value返回当前窗口的第一条记录和最后一条记录
SELECT prod_id,
  first_value(prod_list_price) over( order by prod_id rows BETWEEN 1 preceding AND 1 following) pre_prod_price,
  prod_list_price,
  last_value(prod_list_price) over( order by prod_id rows BETWEEN 1 preceding AND 1 following) next_prod_price
FROM products;
   PROD_ID PRE_PROD_PRICE PROD_LIST_PRICE NEXT_PROD_PRICE
---------- -------------- --------------- ---------------
        13         899.99          899.99          999.99
        14         899.99          999.99          999.99
        15         999.99          999.99          299.99
        16         999.99          299.99         1099.99
        17         299.99         1099.99         1299.99
        18        1099.99         1299.99           55.99
        19        1299.99           55.99          599.99
        20          55.99          599.99          899.99
        21         599.99          899.99           24.99
        22         899.99           24.99           21.99
        23          24.99           21.99           45.99
        24          21.99           45.99          112.99
        25          45.99          112.99          149.99
        26         112.99          149.99           44.99
        27         149.99           44.99          199.99
        28          44.99          199.99          499.99
        29         199.99          499.99            9.99
        30         499.99            9.99            8.9