---
title: "oracle   统计每月累积购买人数"
author: "winzenghua"
date: "2009-12-24T10:07:00Z"
tags: []
slug: "oracle"
draft: false
lastmod: "2009-12-24T10:07:00Z"
id: 987826
url: "https://java4gis.iteye.com/blog/987826"
---

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') &lt;= 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 &lt; months\_between(to\_date('2009-12-01','YYYY-MM-DD'),to\_date('2007-01-01','YYYY-MM-DD'))+1  
) oldtable

```

```