create table t_access_times(username string,month string,salary int)
row format delimited fields terminated by ',';

load data local inpath '/root/hivedata/t_access_times.dat' into table t_access_times;

A,2015-01,5
A,2015-01,15
B,2015-01,5
A,2015-01,8
B,2015-01,25
A,2015-01,5
A,2015-02,4
A,2015-02,6
B,2015-02,10
B,2015-02,5

1、第一步,先求个用户的月总金额
select username,month,sum(salary) as salary from t_access_times group by username,month

+-----------+----------+---------+--+
| username | month | salary |
+-----------+----------+---------+--+
| A | 2015-01 | 33 |
| A | 2015-02 | 10 |
| B | 2015-01 | 30 |
| B | 2015-02 | 15 |
+-----------+----------+---------+--+

2、第二步,将月总金额表 自己连接 自己连接
select A.*,B.* FROM
(select username,month,sum(salary) as salary from t_access_times group by username,month) A
inner join
(select username,month,sum(salary) as salary from t_access_times group by username,month) B
on
A.username=B.username
where B.month <= A.month
+-------------+----------+-----------+-------------+----------+-----------+--+
| a.username | a.month | a.salary | b.username | b.month | b.salary |
+-------------+----------+-----------+-------------+----------+-----------+--+
| A | 2015-01 | 33 | A | 2015-01 | 33 |
| A | 2015-01 | 33 | A | 2015-02 | 10 |
| A | 2015-02 | 10 | A | 2015-01 | 33 |
| A | 2015-02 | 10 | A | 2015-02 | 10 |
| B | 2015-01 | 30 | B | 2015-01 | 30 |
| B | 2015-01 | 30 | B | 2015-02 | 15 |
| B | 2015-02 | 15 | B | 2015-01 | 30 |
| B | 2015-02 | 15 | B | 2015-02 | 15 |
+-------------+----------+-----------+-------------+----------+-----------+--+

3、第三步,从上一步的结果中
进行分组查询,分组的字段是a.username a.month
求月累计值: 将b.month <= a.month的所有b.salary求和即可
select A.username,A.month,max(A.salary) as salary,sum(B.salary) as accumulate
from
(select username,month,sum(salary) as salary from t_access_times group by username,month) A
inner join
(select username,month,sum(salary) as salary from t_access_times group by username,month) B
on
A.username=B.username
where B.month <= A.month
group by A.username,A.month
order by A.username,A.month;

最新文章

  1. java 锁4
  2. 十种MYSQL显错注入原理讲解(一)
  3. java 执行jar指定log4j.properties文件位置
  4. C语言中内存对齐
  5. 处理oracle的死锁
  6. 如何使用mybatis《一》
  7. Java学习记录-注解
  8. python下划线变量的含义
  9. Connection对象连接加密2
  10. 【转】CCUserDefault类深入分析&mdash;&mdash;2013-08-25 22
  11. MySQL添加中文字符集问题 --- java.sql.SQLException: Incorrect string value
  12. shell脚本中常见的一些特殊符号和作用详解
  13. hdu3415(单调队列)
  14. HDU 2732 Leapin&#39; Lizards
  15. 国内为什么没有好的 Stack Overflow 的模仿者?,因为素质太低?没有分享精神?
  16. hive:数据库“行专列”操作---使用collect_set/collect_list/collect_all &amp; row_number()over(partition by 分组字段 [order by 排序字段])
  17. JavaScript实现登录窗口的拖拽
  18. DCDC电源 TPS54525
  19. 8bit数据 转换为 16bit数据的四种方法
  20. django创建app、在视图函数及url中使用参数、url命名、通过redirect实现网页路径跳转

热门文章

  1. 模板——扩展欧几里得算法(求ax+by=gcd的解)
  2. 线段树(单点更新) HDOJ 4288 Coder
  3. oracle中的用户详解 【转】
  4. 使用预定义的action值启动系统应用
  5. Git之远程项目克隆到本地配置
  6. 【学习笔记】深入理解js原型和闭包(17)——补this
  7. Web前端开发学习误区,你掉进去了没?
  8. 基于 Web 的 Go 语言 IDE - Wide 1.5.0 发布!
  9. spring @value 为什么没有获取到值
  10. idea 下maven 导入本地jar,以及导入之后 java不能引用问题