ringovski - Wednesday, May 23, 2018 12:57 AM
Is this what your looking for?
create table customers
(
custid int,
visit_date date
);
insert into customers values(1,'01-13-2017');
insert into customers values (1,'02-25-2017');
select
custid,
avg(datediff(day,visit_date,mx))
from
(select
custid,
visit_date,
max(visit_date) over (partition by custid order by visit_date desc) as mx,
row_number ()over(partition by custid order by visit_date desc) as scmx
from customers)avg_visit
where scmx=2
group by custid
Saravanan