sql笔记 查询连续登陆7天以上的用户

1. 10分钟之内在超过两个医院就诊

--明细
create table lu_tmp as
select t.*, row_number() over(partition by aac147,ymd order by opdate) row_num
from shen_zx_mdps t where ym = '2018-12'

--查一天内重复的
create table lu_tmp1 as
select aac147,opdate,hicode from lu_tmp 
where (aac147,ymd) in (select aac147,ymd from lu_tmp group by aac147,ymd having count(distinct hicode)>2)
group by aac147,opdate,hicode

--查明细
select a.* from lu_tmp a, (
  select a.aac147,a.opdate,a.hicode from lu_tmp1 a, 
  (
         select * 
         from lu_tmp1 t  
         where (
           select count(distinct hicode) 
           from lu_tmp1 
           where aac147=t.aac147 
           and opdate>=t.opdate 
           and opdate<t.opdate+1/144
         )>2
  ) b where a.aac147=b.aac147 and a.opdate>=b.opdate and a.opdate<b.opdate+1/144
) b
where a.aac147=b.aac147 and a.opdate=b.opdate and a.hicode=b.hicode
order by a.aac147,a.opdate,a.hicode

--测试
select aac147,opdate,hicode,hiname from lu_tmp 
where aac147='xx' 
and opdate >= to_date('2018/12/7 10:16:03','yyyy-mm-dd hh24:mi:ss')
and opdate < to_date('2018/12/7 10:16:03','yyyy-mm-dd hh24:mi:ss')+1/144

  2. 查询连续登陆7天以上的用户

1. 排序
2. 日期减序号
3. 人,日期分组统计,大于7
4. 筛选明细

  

猜你喜欢

转载自www.cnblogs.com/iupoint/p/12673760.html
今日推荐