假设我有2个字段a 和 b都是varchar2(6)其中a字段中存储的是结算期:
比如:
201002
201102
201102
201202
201202b里面存的是所属期
比如
201012
201111
201112
201201
201202我怎么求出 b列中最近一次连续月数 对应a列中的最小日期:
比方上例中 b列里面最近一次连续是 201111、201112、201201、201202这四行,对应A列里面min(a)是201102
请问这个sql怎么写,感谢!
比如:
201002
201102
201102
201202
201202b里面存的是所属期
比如
201012
201111
201112
201201
201202我怎么求出 b列中最近一次连续月数 对应a列中的最小日期:
比方上例中 b列里面最近一次连续是 201111、201112、201201、201202这四行,对应A列里面min(a)是201102
请问这个sql怎么写,感谢!
insert into t1 values (1,to_date('201002','yyyymm'));
insert into t1 values (2,to_date('201102','yyyymm'));
insert into t1 values (3,to_date('201102','yyyymm'));
insert into t1 values (4,to_date('201202','yyyymm'));
insert into t1 values (5,to_date('201202','yyyymm'));create table t2 (id number(5),t_date date);
insert into t2 values (1,to_date('201002','yyyymm'));
insert into t2 values (2,to_date('201111','yyyymm'));
insert into t2 values (3,to_date('201112','yyyymm'));
insert into t2 values (4,to_date('201201','yyyymm'));
insert into t2 values (5,to_date('201202','yyyymm'));
commit;select max(to_char(t1.t_date,'yyyymm')) t_date
from
(
select max(t_date) t_date
from
(
select count(*) over (PARTITION by d_time) d_count,t_date
from
(
SELECT id,t_date ,add_months(t_date,-ROW_NUMBER() OVER(ORDER BY t_date)) d_time
FROM t2 t
)
)
where d_count > 1
) a,t1
where t1.t_date<a.t_date t_date
---------------------
1 201102
我想问下 如果如果是这样的数据呢table A
200802 200811
200802 200812
201002 201001
201002 201002
201002 201003
201002 201004
201002 201005
201002 201006
201002 201007
201002 201008
201002 201009
201002 201010
201002 201011
201002 201012
201102 201101
201102 201102
201102 201103
201102 201104
201102 201105
201102 201106
201102 201107
201102 201108
201102 201109
201102 201110
201102 201111
201102 201112
201202 201201
201202 201202
201202 201203
201202 201204
201202 201205
201202 201206
201202 201207
201202 201208
201202 201209
201202 201210
201202 201211
201202 201212第一列是交钱时间,第二列是交钱对应的月份:
上述看到200802月交了2个月后来一直没交钱,到2010年2月交了2010年一年
2011年2月交了2011年1年,2012年交了2012年1年,可以看到2010年以后对应的交钱月份都是连续的,
我想取的是这段连续缴费,最早是什么时间交的,也就是201002这个怎么取出来!
貌似你上述取出来的是201102,麻烦看下是不是这样的?
from
(
select count(*) over (PARTITION by d_time) d_count,t_date
from
(
SELECT id,t_date ,add_months(t_date,-ROW_NUMBER() OVER(ORDER BY t_date)) d_time
FROM t2 t
)
)
where d_count > 1
create table t1 (id number(5),t_date date,t_mon date);
insert into t1 values (1,to_date('201002','yyyymm'),to_date('201002','yyyymm'));
insert into t1 values (1,to_date('201002','yyyymm'),to_date('201003','yyyymm'));
insert into t1 values (2,to_date('201202','yyyymm'),to_date('201111','yyyymm'));
insert into t1 values (3,to_date('201202','yyyymm'),to_date('201112','yyyymm'));
insert into t1 values (4,to_date('201202','yyyymm'),to_date('201201','yyyymm'));
insert into t1 values (5,to_date('201202','yyyymm'),to_date('201202','yyyymm'));
commit;select to_char(t1.t_date,'yyyymm') t_date
from t1
where t_mon=
(select max(t_mon) t_mon
from
(
select t_mon,count(*) over (PARTITION by d_time) d_count
from
(
SELECT t_mon,add_months(t_mon,-ROW_NUMBER() OVER(ORDER BY t_mon)) d_time
FROM t1 t
)
)
where d_count>1)
t_date
---------------------
1 201102
这个应该可行,就是我对分析函数不太理解。。~~~能不能解释下
SELECT id,t_date ,add_months(t_date,-ROW_NUMBER() OVER(ORDER BY t_date)) d_time
FROM t2 t
这句话是啥意思啊,感谢~~~
201111 1
201112 2
201201 3add_months(t_date,-n)将每个月份减去对应的编号
如 11月-1=10月 12月-2=10月 1月-3=10月 根据这个结果可以将连续的月份获取出来