27,580
社区成员
发帖
与我相关
我的任务
分享
with t as(select distinct 日期,账号 from #t where 交易方式='现金' and 借贷标识='贷'),
u as(select rn=datediff(d,'19700101',日期)-dense_rank() over(partition by 账号 order by 日期),日期,账号 from t),
v as(select a.账号,a.日期
from u a
inner join (select 账号,rn from u group by 账号,rn having count(1)>=3) b on a.账号=b.账号 and a.rn=b.rn)
select a.日期,a.时间,a.账号,a.户名,a.金额,a.交易方式,a.借贷标识
from #t a
inner join v b on a.账号=b.账号 and a.日期=b.日期
where a.交易方式='现金' and a.借贷标识='贷'
with t as(
select *,rn=datediff(d,'19700101',日期)-row_number() over(partition by 账号 order by 日期)
from #t
where 交易方式='现金' and 借贷标识='贷')
select 日期,时间,账号,户名,金额,交易方式,借贷标识
from t
where rn in(select rn from t group by rn having count(1)>=3)
/*
日期 时间 账号 户名 金额 交易方式 借贷标识
---------- ---------- ---------- ---------- ----------- ---------- ----------
20150108 090100 14050427 张三 20550 现金 贷
20150109 090101 14050427 张三 24188 现金 贷
20150110 090102 14050427 张三 5000 现金 贷
20150117 090105 14050429 李四 3400 现金 贷
20150118 090106 14050429 李四 600 现金 贷
20150119 090107 14050429 李四 890 现金 贷
20150120 090109 14050429 李四 8000 现金 贷
(7 row(s) affected)
*/
with t as(
select *,rn=datediff(d,'19700101',日期)-dense_rank() over(partition by 账号 order by 日期)
from #t
where 交易方式='现金' and 借贷标识='贷')
select 日期,时间,账号,户名,金额,交易方式,借贷标识
from t
where rn in(select rn from t group by rn having count(1)>=3)
/*
日期 时间 账号 户名 金额 交易方式 借贷标识
---------- ---------- ---------- ---------- ----------- ---------- ----------
20150108 090100 14050427 张三 20550 现金 贷
20150108 090600 14050427 张三 56000 现金 贷
20150108 090600 14050427 张三 66000 现金 贷
20150109 090101 14050427 张三 24188 现金 贷
20150110 090102 14050427 张三 5000 现金 贷
20150117 090105 14050429 李四 3400 现金 贷
20150117 090123 14050429 李四 5400 现金 贷
20150118 090125 14050429 李四 5000 现金 贷
20150118 090106 14050429 李四 600 现金 贷
20150119 090107 14050429 李四 890 现金 贷
20150120 090109 14050429 李四 8000 现金 贷
(11 row(s) affected)
*/