lag查询疑问
--测试数据--
create table lag_test (
area VARCHAR2(20),
arttype VARCHAR2(10),
allcount NUMBER(8),
acct_month VARCHAR2(7)
);
insert into lag_test
select '北京', '说', 15, '201201' from dual
union all
select '北京', '说', 12, '201202' from dual
union all
select '北京', '说', 21, '201203' from dual
union all
select '北京', '说', 31, '201204' from dual
union all
select '北京', '学', 14, '201201' from dual
union all
select '北京', '学', 12, '201202' from dual
union all
select '北京', '学', 21, '201203' from dual
union all
select '北京', '学', 15, '201204' from dual
union all
select '北京', '逗', 15, '201201' from dual
union all
select '北京', '逗', 16, '201202' from dual
union all
select '北京', '逗', 17, '201203' from dual
union all
select '北京', '逗', 18, '201204' from dual
select t.area,
t.arttype,
t.allcount,
t.acct_month,
lag(t.allcount, 1, 0) over(partition by area, arttype order by acct_month) as pre_month_allcount
from lag_test t
where t.acct_month='201203'
--查询1结果--
AREAARTTYPEALLCOUNT ACCT_MONTHPRE_MONTH_ALLCOUNT
1北京逗17 2012030
2北京说21 2012030
3北京学21 2012030
select * from
(select t.area,
t.arttype,
t.allcount,
t.acct_month,
lag(t.allcount, 1, 0) over(partition by area, arttype order by acct_month) as pre_month_allcount
from lag_test t)
where acct_month='201203'
--查询2结果--
AREAARTTYPEALLCOUNTACCT_MONTHPRE_MONTH_ALLCOUNT
1北京逗17 20120316
2北京说21 20120312
3北京学21 20120312