Oracle返回指定列首行或末行值之FIRST_VALUE与LAST_VALUE

FIRST_VALUE与LAST_VALUE功能:返回指定列首行值和末行值

在语法上有9i和10g和区别:

9i:

FIRST_VALUE ( expr ) OVER ( analytic_clause )
10g:
FIRST_VALUE (expr [ IGNORE NULLS ]) OVER (analytic_clause)
如果排序组中的第一个数据为空,oracle会返回空,除非指定IGNORE NULLS。

我的应用:

要完成的功能:汇总统计后,返回最后一个日期。

SQL:关键在于FIRST_VALUE() OVER()的应用。

select count(sec_code) sum_cn, sec_code, sec_sname, nvl(sum(b_sum),0) b_sum, 
nvl(sum(s_sum),0) s_sum, nvl(sum(bs_sum),0) bs_sum, nvl(sum(net_buy),0) net_buy, tradedate from (
select distinct t.sec_code, t.sec_sname, FIRST_VALUE(tradedate) OVER (PARTITION BY sec_code ORDER BY tradedate desc) tradedate, 
nvl(sum(t.buy_value) over(partition by sec_code,tradedate),0) b_sum, nvl(sum(t.sale_value) over(partition by sec_code,tradedate),0) s_sum,  
(nvl(sum(t.buy_value) over(partition by sec_code,tradedate),0)+nvl(sum(t.sale_value) over(partition by sec_code,tradedate),0)) bs_sum, 
(nvl(sum(t.buy_value) over(partition by sec_code,tradedate),0)-nvl(sum(t.sale_value) over(partition by sec_code,tradedate),0)) net_buy from mv_stk_trans_info t 
where to_char(t.tradedate,'yyyy-mm-dd')<=to_char(sysdate,'yyyy-mm-dd') and to_char(t.tradedate,'yyyy-mm-dd')>=to_char(sysdate-30,'yyyy-mm-dd') 
and sec_code='600638' ) co group by sec_code,sec_sname,tradedate order by sum_cn desc


实现:先进行B_SUM,S_SUM_BS_SUM_NET_BUY按SEC_CODE汇总,再记入2012-11-20里

1.sql语句中,注释如下代码结果

 FIRST_VALUE(tradedate) OVER (PARTITION BY sec_code ORDER BY tradedate desc) tradedate

2. 取消对1中的注释结果:


3.






你可能感兴趣的:(软件研发,DB/SQL/NoSQL,JavaEE/Java,C#/.net)