不应该first_value()有序降序给出与last_value()有序升序相同的结果吗?我运行了以下查询,并在查询下方附加了结果。
select random_date, trunc(random_date,'MM') random_month, random_value,
first_value(random_value) over (partition by trunc(random_date,'MM') order by random_date desc) first_,
last_value(random_value) over (partition by trunc(random_date,'MM') order by random_date) last_
from
(select to_date(round (dbms_random.value (1, 28))
|| '-'
|| round (dbms_random.value (02, 03))
|| '-'
|| round (dbms_random.value (2014, 2014)),
'DD-MM-YYYY') + level - 1 random_date,
round(100*(dbms_random.value)) random_value
from dual
connect by level <= 10) order by 2, 1;
答案 0 :(得分:3)
您忘记了用于分析功能的移动窗口。查看差异(无界前后和无界后续之间使用的行):
SQL> select random_date, trunc(random_date,'MM') random_month, random_value,
2 first_value(random_value) over (partition by trunc(random_date,'MM') order by random_date desc rows between unbounded preceding and unbounded following) first_,
3 last_value(random_value) over (partition by trunc(random_date,'MM') order by random_date rows between unbounded preceding and unbounded following) last_
4 from
5 (select to_date(round (dbms_random.value (1, 28))
6 || '-'
7 || round (dbms_random.value (02, 03))
8 || '-'
9 || round (dbms_random.value (2014, 2014)),
10 'DD-MM-YYYY') + level - 1 random_date,
11 round(100*(dbms_random.value)) random_value
12 from dual
13 connect by level <= 10) order by 2, 1;
RANDOM_DATE RANDOM_MONTH RANDOM_VALUE FIRST_ LAST_
----------- ------------ ------------ ---------- ----------
02.02.2014 01.02.2014 93 75 75
09.02.2014 01.02.2014 78 75 75
11.02.2014 01.02.2014 69 75 75
12.02.2014 01.02.2014 13 75 75
21.02.2014 01.02.2014 91 75 75
25.02.2014 01.02.2014 75 75 75
01.03.2014 01.03.2014 54 80 80
15.03.2014 01.03.2014 37 80 80
16.03.2014 01.03.2014 92 80 80
17.03.2014 01.03.2014 80 80 80
10 rows selected
答案 1 :(得分:0)
尝试此分析函数。要实现您的目标,请使用rows between unbounded preceding and unbounded
。
first_value(random_value) over (partition by trunc(random_date,'MM') order by random_date desc ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) first_,
last_value(random_value) over (partition by trunc(random_date,'MM') order by random_date ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) last_
完整代码:
select random_date, trunc(random_date,'MM') random_month, random_value,
first_value(random_value) over (partition by trunc(random_date,'MM') order by random_date desc ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) first_,
last_value(random_value) over (partition by trunc(random_date,'MM') order by random_date ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) last_
from
(select to_date(round (dbms_random.value (1, 28))
|| '-'
|| round (dbms_random.value (02, 03))
|| '-'
|| round (dbms_random.value (2014, 2014)),
'DD-MM-YYYY') + level - 1 random_date,
round(100*(dbms_random.value)) random_value
from dual
connect by level <= 10) order by 2, 1;
答案 2 :(得分:0)
正如我看到你做的first_value这只用于字符串,但是你需要一个数字,所以把它改成min和max 在字符串中,10将在2之前,但在数字2之前是