ORACLE 獲取同期資料的兩種方法

2021-06-28 19:42:33 字數 871 閱讀 3724

首先,建立一些test_table的測試表,這個表有三個字段,分別為區域,年月和數量。

sql語句如下:

create table test_table(

region varchar2(20),

yearmouth number(6),

amount number(10,2)

)再插入一些測試資料,如下:

第一種方法,可以用lag函式倆獲取上個月的資料,先新增乙個上月的數量字段,sql語句如下:

merge into test_table t1

using (select 

t2.region,

t2.yearmouth,

lag(t2.amount, 1, null) over(partition by t2.region order by t2.yearmouth) as last_mouth_amout

from test_table t2

) t3

on (t1.region = t3.region and t1.yearmouth = t3.yearmouth)

when matched then

update set 

t1.last_mouth_amount = t3.last_mouth_amout;

commit;

得到的結果如下:

第二種方法,將年月這個字段減去1,得到上個月,sql語句如下:

select t1.yearmouth - 1 as yearmouth,

t1.region as region,

t1.amount as last_month_amount

from test_table t1;

結果如下:

兩種獲取Oracle Sequence的方法

前提 create table booking id integer not null,date made date,reserved until timestamp,price decimal 15,2 not null,purchase id integer,primary key id cre...

hibernate兩種獲取session方法的區別

在hibernate中有兩種方法獲得session opensession getcurrentsession 如果使用的getcurrentsession 方法 就要在hibernate.cfg.xml檔案中進行配置 如果是本地事務 jdbc thread 如果是全域性事物 jta jta 兩種方...

oracle分頁的兩種方式

方式一 select from select rownum r e.empno from select from emp order by sal desc e where r 5 and r 8 注 在oracle中rownum永遠是從1開始的,所以where條件不能 使用 比如 蓋8層樓,123...