需求:

有如下访客访问次数统计表 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

……

……

……

需要输出报表:t_access_times_accumulate

访客

月份

月访问总计

累计访问总计

A

2015-01

33

33

A

2015-02

10

43

…….

…….

…….

…….

B

2015-01

30

30

B

2015-02

15

45

…….

…….

…….

…….

思路:

1、第一步,先求个用户的月总金额

select username,month,sum(salary) 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) salary from t_access_times group by username,month) A
join
(select username,month,sum(salary) salary from t_access_times group by username,month) B
on
A.username=B.username;

+-------------+----------+-----------+-------------+----------+-----------+--+
| 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) salary,sum(B.salary) accumulate
from
(select username,month,sum(salary) salary from t_access_times group by username,month) A
join
(select username,month,sum(salary) 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;

Hive面试题——累计求和的更多相关文章

  1. SQL集合运算参考及案例(一):列值分组累计求和

    概述 目前企业应用系统使用的大多数据库都是关系型数据库,关系数据库依赖的理论就是针对集合运算的关系代数.关系代数是一种抽象的查询语言,是关系数据操纵语言的一种传统表达方式.不过我们在工作中发现,很多人 ...

  2. oracle累计求和

    //将当前行某列的值与前面所有行的此列值相加,即累计求和: //方法一: with t as(      select 1 val from dual union all      select 3 ...

  3. Hive 笔试题

    Hive 笔试题 考试时间: 姓名:____________ 考试成绩:____________ 考试时长:180 分钟 注意事项: 1. 自主答题,不能参考任何除本试卷外的其它资料. 2. 总成绩共 ...

  4. 数据可视化之DAX篇(十)在PowerBI中累计求和的两种方式

    https://zhuanlan.zhihu.com/p/64418286 假设有一组数据, 已知每一个产品贡献的利润,如果要计算前几名产品的贡献利润总和,或者每一个产品和利润更高产品的累计贡献占总体 ...

  5. 数据可视化之DAX篇(二十三)ALLEXCEPT应用示例:更灵活的累计求和

    https://zhuanlan.zhihu.com/p/67441847 累计求和问题,之前已经介绍过(有了这几个公式,你也可以快速搞定累计求和),主要是基于比较简单的情形,针对所有的数据进行累计求 ...

  6. hive面试题(免费拿走不谢)

    Hive 最常见的几个面试题 1.hive 的使用, 内外部表的区别,分区作用, UDF 和 Hive 优化(1)hive 使用:仓库.工具(2)hive 内部表:加载数据到 hive 所在的 hdf ...

  7. hive面试题

    1. Hive数据倾斜原因: key分布不均匀 业务数据本身的特性 SQL语句造成数据倾斜解决方法hive设置hive.map.aggr=true和hive.groupby.skewindata=tr ...

  8. hive 面试题 转载

    转自:http://blog.csdn.net/ningguixin/article/details/12852051 有一张很大的表:TRLOG该表大概有2T左右TRLOG:CREATE TABLE ...

  9. Storm累计求和进群运行代码

    打成jar包放在主节点上去运行. import java.util.Map; import backtype.storm.Config; import backtype.storm.StormSubm ...

随机推荐

  1. hdu 1540 Tunnel Warfare 线段树 单点更新,查询区间长度,区间合并

    Tunnel Warfare Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...

  2. BFC与hasLayout

    BFC与hasLayout都是CSS布局上的概念. 几个月前在微博上才了解什么是BFC,算是对布局有点初步的了解. hasLayout则是IE6.7产生许多bug的根源. 一.BFC Floats, ...

  3. Nginx反向代理 实现Web负载均衡

    实现负载均衡的方式有很多种,DNS.反向代理.LVS负载均衡器(软件实现).F5(负载均衡器,硬件,非常昂贵)这里我们只提到基于DNS,以及反向代理的方式来实现负载均衡Web服务       DNS服 ...

  4. Spring MVC @ModelAttribute 详解

    1.@ModelAttribute注释void返回值的方法 @Controller public class HelloModelController { @ModelAttribute public ...

  5. linux开放关闭防火墙端口

    原文:http://blog.csdn.net/fengspg/article/details/21337617 1) 重启后生效 开启: chkconfig iptables on 关闭: chkc ...

  6. linux 编译win32程序

    apt-get install mingw32 int main(int argc, char *argv) { printf("Windows Compiler Test\n") ...

  7. gdb对应vc调试命令

    gdb vc调试对照表: 实现功能                vc                   gdb 修改后编译              f7                   ma ...

  8. 卷积神经网络LeNet Convolutional Neural Networks (LeNet)

    Note This section assumes the reader has already read through Classifying MNIST digits using Logisti ...

  9. Python学习(四)数据结构 —— str

    Python 字符串 str 本章大致介绍了 Python 的字符串类型 str,包括字符串的赋值及转义.字符串运算符.字符串格式化输出 以及 一些字符串的内建函数等. 字符串赋值及转义 我们可以使用 ...

  10. 11.线程通信CountDownLatch

    package demo2; import java.util.concurrent.CountDownLatch; /** * Created by liudan on 2017/7/27. */ ...