日期:2014-05-16 浏览次数:20852 次
--为什么需要对日期进行分组呢,你不是统计指定日期前未出现过的人数吗?
with t("date",userid) as(
select to_date('2012-10-01','yyyy-mm-dd'),1 from dual
union all select to_date('2012-10-01','yyyy-mm-dd'),2 from dual
union all select to_date('2012-10-01','yyyy-mm-dd'),3 from dual
union all select to_date('2012-09-22','yyyy-mm-dd'),1 from dual
union all select to_date('2012-08-22','yyyy-mm-dd'),1 from dual
)
select count(distinct userid) total from t where t."date">trunc(sysdate)
and not exists(select 1 from t t1 where t.userid=t1.userid and t1."date"<=trunc(sysdate));
with t (dt, userid) as (
select to_date('2012-10-01', 'yyyy-mm-dd'), 1 from dual
union all select to_date('2012-10-01', 'yyyy-mm-dd'), 2 from dual
union all select to_date('2012-10-01', 'yyyy-mm-dd'), 3 from dual
union all select to_date('2012-09-22', 'yyyy-mm-dd'), 1 from dual
union all select to_date('2012-08-22', 'yyyy-mm-dd'), 1 from dual
)
select distinct(userid) from t where not exists
(select 1 from t t1 where t.userid=t1.userid and t1.dt < to_date('2012-09-30','yyyy-mm-dd'));