日期:2014-05-17 浏览次数:20966 次
create table tabname1 (id1 int ,month1 DATE); create table tabname1 (id0 INT,id1 int ,month1 DATE); insert into tabname1 values(1,1,to_date('2009-01-01','yyyy-mm-dd')); insert into tabname1 values(1,1,to_date('2009-02-01','yyyy-mm-dd')); insert into tabname1 values(1,1,to_date('2009-03-01','yyyy-mm-dd')); insert into tabname1 values(1,2,to_date('2009-04-01','yyyy-mm-dd')); insert into tabname1 values(1,2,to_date('2009-05-01','yyyy-mm-dd')); insert into tabname1 values(1,2,to_date('2009-06-01','yyyy-mm-dd')); insert into tabname1 values(1,1,to_date('2009-07-01','yyyy-mm-dd')); insert into tabname1 values(1,1,to_date('2009-08-01','yyyy-mm-dd')); insert into tabname1 values(2,1,to_date('2009-05-01','yyyy-mm-dd')); insert into tabname1 values(2,1,to_date('2009-06-01','yyyy-mm-dd')); insert into tabname1 values(2,3,to_date('2009-08-01','yyyy-mm-dd')); insert into tabname1 values(2,3,to_date('2009-09-01','yyyy-mm-dd'));
create table tabname1 (id0 INT,id1 int ,month1 DATE); insert into tabname1 values(1,1,to_date('2009-01-01','yyyy-mm-dd')); insert into tabname1 values(1,1,to_date('2009-02-01','yyyy-mm-dd')); insert into tabname1 values(1,1,to_date('2009-03-01','yyyy-mm-dd')); insert into tabname1 values(1,2,to_date('2009-04-01','yyyy-mm-dd')); insert into tabname1 values(1,2,to_date('2009-05-01','yyyy-mm-dd')); insert into tabname1 values(1,2,to_date('2009-06-01','yyyy-mm-dd')); insert into tabname1 values(1,1,to_date('2009-07-01','yyyy-mm-dd')); insert into tabname1 values(1,1,to_date('2009-08-01','yyyy-mm-dd')); insert into tabname1 values(2,1,to_date('2009-05-01','yyyy-mm-dd')); insert into tabname1 values(2,1,to_date('2009-06-01','yyyy-mm-dd')); insert into tabname1 values(2,3,to_date('2009-08-01','yyyy-mm-dd')); insert into tabname1 values(2,3,to_date('2009-09-01','yyyy-mm-dd')); insert into tabname1 values(2,4,to_date('2009-12-01','yyyy-mm-dd')); insert into tabname1 values(2,4,to_date('2010-01-01','yyyy-mm-dd')); select id0,id1,min(month1),max(month1) from (SELECT id0,id1,month1,COUNT(1) over(PARTITION BY id0,id1,dr) dr FROM (SELECT id0,id1, month1, add_months(month1,- DENSE_RANK() OVER(PARTITION BY id0,id1 ORDER BY month1)) dr FROM tabname1 ) ) group by id0,id1,dr order by id0,id1 id0