11

Last_value窗口功能doesn't正常工作。

CREATE TABLE EXAMP2
(
  CUSTOMER_ID  NUMBER(38)                       NOT NULL,
  VALID_FROM   DATE                             NOT NULL
);


Customer_id      Valid_from
-------------------------------------
 9775             06.04.2013 01:34:16
 9775             06.04.2013 20:34:00
 9775             12.04.2013 11:07:01
--------------------------------------

select DISTINCT LAST_VALUE(VALID_FROM) 
  OVER (partition by customer_id ORDER BY VALID_FROM ASC) rn 
from   examp1;

当我使用时LAST_VALUE,我得到以下行:

06.04.2013 20:34:00
06.04.2013 01:34:16
12.04.2013 11:07:01

当我使用时FIRST_VALUE,我得到以下行:

select  DISTINCT FIRST_VALUE(VALID_FROM) 
OVER (partition by customer_id ORDER BY VALID_FROM DESC) rn 
from   examp1;

4/12/2013 11:07:01 AM

First_value查询给出正确的输出。我希望从这些查询中获得相同的输出。为什么我有2 different results

4

2 回答 2

14

在分析函数中,您需要指定窗口范围。默认情况下它是between unbounded preceding and current row,我认为这是不言自明的。

基本上,这就是您指定时发生的情况partition by customer_id order by valid_from asc

  1. Oracle 获取与当前行匹配的所有行customer id
  2. 它按升序排列它们valid_from
  3. 它形成一个以最小valid_from日期开始,以当前行结束的窗口valid_from
  4. 它评估last_value,返回您当前行的valid_from.

您需要做的是指定一个持续的范围:

16:53:00 SYSTEM@sandbox> ed
Wrote file S:\spool\sandbox\BUFFER_SYSTEM_38.sql

  1  select last_value(VALID_FROM) OVER (
  2    partition by customer_id
  3    ORDER BY VALID_FROM asc
  4    range between current row and unbounded following
  5  ) rn
  6* from   t
16:53:21 SYSTEM@sandbox> /

RN
---------------------------------------------------------------------------
04-DEC-13 11.07.01.000000 AM
04-DEC-13 11.07.01.000000 AM
04-DEC-13 11.07.01.000000 AM

Elapsed: 00:00:00.01
于 2013-04-29T08:57:22.913 回答
9

first_value并且last_value有点特别,因为它们需要一个可以操作的窗口。

您需要添加ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING如下:

select DISTINCT LAST_VALUE(VALID_FROM) OVER (partition by customer_id
  ORDER BY VALID_FROM ASC
  ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) rn
from examp1;

请参阅文档:http ://docs.oracle.com/cd/B19306_01/server.102/b14200/functions001.htm#i81407并特别阅读有关窗口的部分。

请注意,接受窗口子句的函数的默认子句RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW解释了为什么一个订单有效而另一个订单无效!默认子句以这种方式设置,以便在无需指定窗口的情况下轻松进行运行总计类型计算。

于 2013-04-29T08:46:16.560 回答