oracle 统计每月累积购买人数

来源:互联网 发布:bilibil直播优化 编辑:程序博客网 时间:2024/05/06 00:19

oracle 可有什么方法 统计每月累积购买人数
比如说有个门店 2008年购买商品的人数
表:t_buy_log  字段 f_username 用户名称;购买时间f_buytime

1 如果是统计每月购买人数
select to_char(f_buytime,'yyymm'),count(distinct f_username)
from t_buy_log
group by to_char(f_buytime,'yyymm')
order by to_char(f_buytime,'yyymm') asc

结果:
1月 100人
2月 150人
3月 50人

2 如果统计每月累积购买人数 ?
1月 100人
2月 是1月和2月的购买人数 客户有重复 值在100-250之间
3月 是1-3月的购买人数

这个该怎么写呢?
用分析函数的SUM 好像不行
1-1
select to_char(f_buytime,'yyymm'),count(distinct f_username)
from t_buy_log
WHERE to_char(f_buytime,'yyymm')='200901'

1-2
select to_char(f_buytime,'yyymm'),count(distinct f_username)
from t_buy_log
WHERE to_char(f_buytime,'yyymm') between '200901'  and '200902'


1-3
select to_char(f_buytime,'yyymm'),count(distinct f_username)
from t_buy_log
WHERE to_char(f_buytime,'yyymm') between '200901'  and '200903'

 

 

 

Create Table t_tmp_test As
select oldtable.tm,
(
 select/*+parallel(t,20)*/ count(distinct f_username)
 from t_Base_Prouser_Suc t where to_char(f_Expectenddate, 'yyyyMM') <= oldtable.tm
 And t.f_Source In ( Select f_buymodeid From T_base_Flat_Type Where f_department = 1)
 ) rs
from
(
select   to_char(add_months(to_date('2007-01-01','YYYY-MM-DD'),(Level-1)),'yyyymm') tm       
from dual
connect by level < months_between(to_date('2009-12-01','YYYY-MM-DD'),to_date('2007-01-01','YYYY-MM-DD'))+1
) oldtable