17,086
社区成员
发帖
与我相关
我的任务
分享
insert table AnalyzeJQ
(
date,
num,
amt,
)
select "20120601",
count(distinct num),
sum(amt)
from t_log
where (date >= "20120601" and date <= "20120630")
with t1 as
(
select date'2012-06-01' c1,'a' c2,100 c3 from dual
union all
select date'2012-06-01' c1,'a' c2,200 c3 from dual
union all
select date'2012-06-10' c1,'b' c2,300 c3 from dual
union all
select date'2012-06-10' c1,'c' c2,400 c3 from dual
union all
select date'2012-06-20' c1,'c' c2,500 c3 from dual
union all
select date'2012-06-25' c1,'c' c2,600 c3 from dual
union all
select date'2012-06-30' c1,'c' c2,700 c3 from dual
union all
select date'2012-06-30' c1,'d' c2,700 c3 from dual
)
select c1,
count(distinct c2) c2,
(select sum(c3) from t1 b where b.c1 <= a.c1) c3
from t1 a
group by c1
c1 c2 c3
------------------------------------------
1 2012/6/1 1 300
2 2012/6/10 2 1000
3 2012/6/20 1 1500
4 2012/6/25 1 2100
5 2012/6/30 2 3500
c1 c2 c3
------------------------------------------
1 2012/6/1 1 300
2 2012/6/10 2 1000
3 2012/6/20 3 1500
4 2012/6/25 3 2100
5 2012/6/30 4 3500
insert table AnalyzeJQ
(
date, -- 日期
num, -- 用户的ID
amt -- 支付金额
)
-- 每天用户支付过就会产生一条上述格式的流水
-- 要统计的格式:
-- 20120701 消费的用户数 消费的总金额
-- 20120702 1号至2号消费的用户数 1号至2号消费的总金额
-- 20120703 1号至3号消费的用户数 1号至3号消费的总金额
即一个叠加的数据,其中消费的用户数要去重
select t2.d_date,nvl(c2,0) c1,sum(c2) over(order by t2.d_date) c2
from
(select date'2012-06-01'+level-1 d_date
from dual
connect by level <= 30) t2 left join t1 on t2.d_date = t1.c1
with t1 as
(
select date'2012-06-01' c1,100 c2 from dual
union all
select date'2012-06-04' c1,200 c2 from dual
union all
select date'2012-06-10' c1,300 c2 from dual
union all
select date'2012-06-15' c1,400 c2 from dual
union all
select date'2012-06-20' c1,500 c2 from dual
union all
select date'2012-06-25' c1,600 c2 from dual
union all
select date'2012-06-30' c1,700 c2 from dual
)
select t2.d_date,sum(c2) over(order by t2.d_date) c2
from
(select date'2012-06-01'+level-1 d_date
from dual
connect by level <= 30) t2 left join t1 on t2.d_date = t1.c1
d_date c2
-------------------------------
1 2012/6/1 100
2 2012/6/2 100
3 2012/6/3 100
4 2012/6/4 300
5 2012/6/5 300
6 2012/6/6 300
7 2012/6/7 300
8 2012/6/8 300
9 2012/6/9 300
10 2012/6/10 600
11 2012/6/11 600
12 2012/6/12 600
13 2012/6/13 600
14 2012/6/14 600
15 2012/6/15 1000
16 2012/6/16 1000
17 2012/6/17 1000
18 2012/6/18 1000
19 2012/6/19 1000
20 2012/6/20 1500
21 2012/6/21 1500
22 2012/6/22 1500
23 2012/6/23 1500
24 2012/6/24 1500
25 2012/6/25 2100
26 2012/6/26 2100
27 2012/6/27 2100
28 2012/6/28 2100
29 2012/6/29 2100
30 2012/6/30 2800
select c_date,
sum(c_count) over(partition by substr(c_date,1,6) --按月分组
order by c_date) as data_count
from
(select c_date,
count(*) as c_count)
from tab
group by c_date)
with t1 as
(
select date'2012-06-01' c1,'a' c2,100 c3 from dual
union all
select date'2012-06-01' c1,'a' c2,200 c3 from dual
union all
select date'2012-06-10' c1,'b' c2,300 c3 from dual
union all
select date'2012-06-10' c1,'c' c2,400 c3 from dual
union all
select date'2012-06-20' c1,'c' c2,500 c3 from dual
union all
select date'2012-06-25' c1,'c' c2,600 c3 from dual
union all
select date'2012-06-30' c1,'c' c2,700 c3 from dual
union all
select date'2012-06-30' c1,'d' c2,700 c3 from dual
)
select t2.d_date,
nvl(c2,0) c2,
nvl(c3,0) c3,
sum(c3) over(order by t2.d_date) s_c3
from
(
select date'2012-06-01'+level-1 d_date
from dual
connect by level <= 30
) t2 left join
(
select c1,count(distinct c2) c2,sum(c3) c3
from t1
group by c1
) t1
on t2.d_date = t1.c1
d_date c2 c3 s_c3
---------------------------------------------------
1 2012/6/1 1 300 300
2 2012/6/2 0 0 300
3 2012/6/3 0 0 300
4 2012/6/4 0 0 300
5 2012/6/5 0 0 300
6 2012/6/6 0 0 300
7 2012/6/7 0 0 300
8 2012/6/8 0 0 300
9 2012/6/9 0 0 300
10 2012/6/10 2 700 1000
11 2012/6/11 0 0 1000
12 2012/6/12 0 0 1000
13 2012/6/13 0 0 1000
14 2012/6/14 0 0 1000
15 2012/6/15 0 0 1000
16 2012/6/16 0 0 1000
17 2012/6/17 0 0 1000
18 2012/6/18 0 0 1000
19 2012/6/19 0 0 1000
20 2012/6/20 1 500 1500
21 2012/6/21 0 0 1500
22 2012/6/22 0 0 1500
23 2012/6/23 0 0 1500
24 2012/6/24 0 0 1500
25 2012/6/25 1 600 2100
26 2012/6/26 0 0 2100
27 2012/6/27 0 0 2100
28 2012/6/28 0 0 2100
29 2012/6/29 0 0 2100
30 2012/6/30 2 1400 3500