例如:學生A有三條記錄,三個時間分別為2017-1-1,2017-3-1,2017-10-1,
那么連續報讀次數為1;
學生B有三條記錄:三個時間分別為2017-1-1,2017-3-1,2017-8-30,
那么連續報讀次數為2;
求help!!
uj5u.com熱心網友回復:
with t1 as
(
select 'A' c1, '2017-1-1' c2 from dual union all
select 'A' c1, '2017-10-1' c2 from dual union all
select 'A' c1, '2017-3-1' c2 from dual union all
select 'B' c1, '2017-1-1' c2 from dual union all
select 'B' c1, '2017-3-1' c2 from dual union all
select 'B' c1, '2017-8-30' c2 from dual
)
, t2 as
(
select row_number() over(order by c1, to_date(c2, 'yyyy-mm-dd')) rn, c1, to_date(c2, 'yyyy-mm-dd') c2 from t1
)
select a.c1, sum(case when b.c1 is not null and months_between(b.c2, a.c2)<=6 then 1 else 0 end)
from t2 a
left join t2 b on a.c1=b.c1 and a.rn=b.rn-1
group by a.c1
uj5u.com熱心網友回復:
有點強
,但是有個問題,這樣求的是所有連續報讀次之和,我這個需求想求的是最大連續次數當5個時間分別為2017-1-1,
2017-3-1,
2017-10-1,
2017-11-1,
2018-8-1的時候,應該為2,而不是3
uj5u.com熱心網友回復:
有點強,但是有個問題,這樣求的是所有連續報讀次之和,我這個需求想求的是最大連續次數當5個時間分別為2017-1-1,
2017-3-1,
2017-10-1,
2017-11-1,
2018-8-1的時候,應該為2,而不是3
uj5u.com熱心網友回復:
這5個日期,計算出來應該也是2啊。
除非是2017-1-1、2017-3-1、2017-10-1、2017-11-1、2017-12-1這樣的5個日期,前2個一組,后3個一組,想要計算出結果是2。
uj5u.com熱心網友回復:
仔細想了一下,感覺邏輯還是不對。
uj5u.com熱心網友回復:
with t1 as(
select 'A' id1, date'2017-1-1' start_date from dual union all
select 'A' id1, date'2017-8-1' start_date from dual union all
select 'A' id1, date'2017-10-1' start_date from dual union all
select 'A' id1, date'2017-11-1' start_date from dual union all
select 'A' id1, date'2017-12-1' start_date from dual union all
select 'A' id1, date'2018-7-1' start_date from dual union all
select 'A' id1, date'2018-10-1' start_date from dual union all
select 'A' id1, date'2018-11-1' start_date from dual union all
select 'B' id1, date'2017-12-1' start_date from dual union all
select 'B' id1, date'2018-7-1' start_date from dual union all
select 'B' id1, date'2018-10-1' start_date from dual union all
select 'B' id1, date'2018-11-1' start_date from dual UNION ALL
select 'B' id1, date'2019-10-1' start_date from dual union all
select 'B' id1, date'2020-11-1' start_date from dual
)
, t2 as
(select id1
,start_date start_day
,nvl(lead(start_date) over(partition by id1 order by start_date),date'2999-12-31') end_day
,row_number() over(partition by id1 order by start_date) rn
from t1)
select
id1
,max(num1)+1 --最大連續次數
from (
SELECT ID1,TYPE1,COUNT(1) num1
FROM (select rn-rn1 TYPE1
,a1.*
from (
select a.*
,row_number() over(partition by id1 order by start_day) rn1
from (select t2.id1
,t2.rn
,t2.start_day
,t2.end_day
,case when add_months(t2.start_day,6) > t2.end_day then 1 else 0 end lx_rn
from t2
where case when add_months(t2.start_day,6) > t2.end_day then 1 else 0 end > 0
) a
) a1
) A2
GROUP BY ID1,TYPE1)
group by id1
;
看看這種
uj5u.com熱心網友回復:
with t1 as
(
select 'A' id1, date'2017-1-1' start_date from dual union all
select 'A' id1, date'2017-8-1' start_date from dual union all
select 'A' id1, date'2017-10-1' start_date from dual union all
select 'A' id1, date'2017-11-1' start_date from dual union all
select 'A' id1, date'2017-12-1' start_date from dual union all
select 'A' id1, date'2018-7-1' start_date from dual union all
select 'A' id1, date'2018-10-1' start_date from dual union all
select 'A' id1, date'2018-11-1' start_date from dual union all
select 'B' id1, date'2017-12-1' start_date from dual union all
select 'B' id1, date'2018-7-1' start_date from dual union all
select 'B' id1, date'2018-10-1' start_date from dual union all
select 'B' id1, date'2018-11-1' start_date from dual UNION ALL
select 'B' id1, date'2019-10-1' start_date from dual union all
select 'B' id1, date'2020-11-1' start_date from dual
)
, t2 as
(select id1
,start_date start_day
,nvl(lead(start_date) over(partition by id1 order by start_date),date'2999-12-31') end_day
,row_number() over(partition by id1 order by start_date) rn
from t1)
select
id1
,max(num1) --最大連續次數
from (
SELECT ID1,TYPE1,COUNT(1) num1
FROM (select rn-rn1 TYPE1
,a1.*
from (
select a.*
,row_number() over(partition by id1 order by start_day) rn1
from (select t2.id1
,t2.rn
,t2.start_day
,t2.end_day
,case when add_months(t2.start_day,6) > t2.end_day then 1 else 0 end lx_rn
from t2
where case when add_months(t2.start_day,6) > t2.end_day then 1 else 0 end > 0
) a
) a1
) A2
GROUP BY ID1,TYPE1)
group by id1
;
不需要加1了的,看成連續次數了uj5u.com熱心網友回復:
如何算連續報讀??轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/65119.html
標籤:開發
