前言:前段时间看到MariaDB10.2出测试版本了,心想有什么新特性玩玩,大家都知道MySQL不支持分析函数,但是MariaDB10.2.X支持分析函数了,

1.表结构

CREATE TABLE `t1` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`userId` varchar(30) NOT NULL COMMENT '用户ID',

`orderId` varchar(60) NOT NULL DEFAULT '' COMMENT '订单ID',

`channerId` varchar(60) NOT NULL DEFAULT '' COMMENT '充值渠道',

`amount` int(11) DEFAULT NULL COMMENT '充值金额',

PRIMARY KEY (`id`),

KEY `idx_userid` (`userId`),

KEY `idx_channerId` (`channerId`)

) ENGINE=InnoDB  DEFAULT CHARSET=utf8

2.插入测试数据

insert into t1 (userId, orderId, channerId, amount) values('张3','2016060101','支付宝',100);

insert into t1 (userId, orderId, channerId, amount) values('李4','2016060102','支付宝',98);

insert into t1 (userId, orderId, channerId, amount) values('张3','2016060103','易票联',60);

insert into t1 (userId, orderId, channerId, amount) values('张3','2016060104','银联',120);

insert into t1 (userId, orderId, channerId, amount) values('王5','2016060105','微信',170);

insert into t1 (userId, orderId, channerId, amount) values('张3','2016060106','微信',40);

insert into t1 (userId, orderId, channerId, amount) values('张3','2016060107','支付宝',200);

insert into t1 (userId, orderId, channerId, amount) values('张3','2016060108','易支付',10);

insert into t1 (userId, orderId, channerId, amount) values('张3','2016060109','微信','80);

insert into t1 (userId, orderId, channerId, amount) values('李4','2016060110','支付宝',300);

insert into t1 (userId, orderId, channerId, amount) values('李4','2016060111','银联',40);

insert into t1 (userId, orderId, channerId, amount) values('李4','2016060112','钱包',72);

insert into t1 (userId, orderId, channerId, amount) values('赵6','2016060113','闪付',20);

insert into t1 (userId, orderId, channerId, amount) values('田7','2016060114','钱包',80);

insert into t1 (userId, orderId, channerId, amount) values('刘1','2016060115','微信',60);

insert into t1 (userId, orderId, channerId, amount) values('哈2','2016060116','易支付',82);

3.查询每个充值渠道的总金额

MariaDB [test11]> select channerId,sum(amount) over(partition by channerId) from t1;

+-----------+------------------------------------------+

| channerId | sum(amount) over(partition by channerId) |

+-----------+------------------------------------------+

| 支付宝    |                                      698 |

| 支付宝    |                                      698 |

| 易票联    |                                       60 |

| 银联      |                                      160 |

| 微信      |                                      350 |

| 微信      |                                      350 |

| 支付宝    |                                      698 |

| 易支付    |                                       92 |

| 微信      |                                      350 |

| 支付宝    |                                      698 |

| 银联      |                                      160 |

| 钱包      |                                      152 |

| 闪付      |                                       20 |

| 钱包      |                                      152 |

| 微信      |                                      350 |

| 易支付    |                                       92 |

+-----------+------------------------------------------+

16 rows in set (0.02 sec)

MariaDB [test11]> select * from (

-> select channerId,sum(amount) over(partition by channerId) from t1) a

->  GROUP BY channerId;

+-----------+------------------------------------------+

| channerId | sum(amount) over(partition by channerId) |

+-----------+------------------------------------------+

| 微信      |                                      350 |

| 支付宝    |                                      698 |

| 易支付    |                                       92 |

| 易票联    |                                       60 |

| 钱包      |                                      152 |

| 银联      |                                      160 |

| 闪付      |                                       20 |

+-----------+------------------------------------------+

7 rows in set (0.02 sec)

4.查询每个渠道平均充值金额

MariaDB [test11]> select channerId,avg(amount) over(partition by channerId) from t1

-> ;

+-----------+------------------------------------------+

| channerId | avg(amount) over(partition by channerId) |

+-----------+------------------------------------------+

| 支付宝    |                                 174.5000 |

| 支付宝    |                                 174.5000 |

| 易票联    |                                  60.0000 |

| 银联      |                                  80.0000 |

| 微信      |                                  87.5000 |

| 微信      |                                  87.5000 |

| 支付宝    |                                 174.5000 |

| 易支付    |                                  46.0000 |

| 微信      |                                  87.5000 |

| 支付宝    |                                 174.5000 |

| 银联      |                                  80.0000 |

| 钱包      |                                  76.0000 |

| 闪付      |                                  20.0000 |

| 钱包      |                                  76.0000 |

| 微信      |                                  87.5000 |

| 易支付    |                                  46.0000 |

+-----------+------------------------------------------+

16 rows in set (0.01 sec)

MariaDB [test11]> select * from (

-> select channerId,avg(amount) over(partition by channerId) from t1) a

-> GROUP BY channerId;

+-----------+------------------------------------------+

| channerId | avg(amount) over(partition by channerId) |

+-----------+------------------------------------------+

| 微信      |                                  87.5000 |

| 支付宝    |                                 174.5000 |

| 易支付    |                                  46.0000 |

| 易票联    |                                  60.0000 |

| 钱包      |                                  76.0000 |

| 银联      |                                  80.0000 |

| 闪付      |                                  20.0000 |

+-----------+------------------------------------------+

7 rows in set (0.00 sec)

5.查询每个渠道最小充值金额

MariaDB [test11]> select * from ( select channerId,min(amount) over(partition by channerId) from t1) a   GROUP BY channerId;

ERROR 1235 (42000): This version of MariaDB doesn't yet support 'This aggregate as window function'

可惜不支持

6.查询每个驱动最大充值金额

MariaDB [test11]> select * from ( select channerId,max(amount) over(partition by channerId) from t1) a   GROUP BY channerId;

ERROR 1235 (42000): This version of MariaDB doesn't yet support 'This aggregate as window function'

MariaDB [test11]>

聪明的你,一下子猜测到不支持

#7.根据每个充值渠道,根据充值金额排名

MariaDB [test11]> SELECT RANK() OVER (PARTITION BY channerId ORDER BY amount desc ) AS rank,     DENSE_RANK() OVER (PARTITION BY channerId ORDER BY amount desc ) AS dense_rank,     ROW_NUMBER() OVER (PARTITION BY channerId ORDER BY amount desc) AS row_num, userId,channerId,amount    from t1 ;

+------+------------+---------+--------+-----------+--------+

| rank | dense_rank | row_num | userId | channerId | amount |

+------+------------+---------+--------+-----------+--------+

|    3 |          3 |       3 | 张3    | 支付宝    |    100 |

|    4 |          4 |       4 | 李4    | 支付宝    |     98 |

|    1 |          1 |       1 | 张3    | 易票联    |     60 |

|    1 |          1 |       1 | 张3    | 银联      |    120 |

|    1 |          1 |       1 | 王5    | 微信      |    170 |

|    4 |          4 |       4 | 张3    | 微信      |     40 |

|    2 |          2 |       2 | 张3    | 支付宝    |    200 |

|    2 |          2 |       2 | 张3    | 易支付    |     10 |

|    2 |          2 |       2 | 张3    | 微信      |     80 |

|    1 |          1 |       1 | 李4    | 支付宝    |    300 |

|    2 |          2 |       2 | 李4    | 银联      |     40 |

|    2 |          2 |       2 | 李4    | 钱包      |     72 |

|    1 |          1 |       1 | 赵6    | 闪付      |     20 |

|    1 |          1 |       1 | 田7    | 钱包      |     80 |

|    3 |          3 |       3 | 刘1    | 微信      |     60 |

|    1 |          1 |       1 | 哈2    | 易支付    |     82 |

+------+------------+---------+--------+-----------+--------+

16 rows in set (0.00 sec)

#排个序,这样就好看多了,每个渠道充值排名

MariaDB [test11]> SELECT RANK() OVER (PARTITION BY channerId ORDER BY amount desc ) AS rank,     DENSE_RANK() OVER (PARTITION BY channerId ORDER BY amount desc ) AS dense_rank,     ROW_NUMBER() OVER (PARTITION BY channerId ORDER BY amount desc) AS row_num, userId,channerId,amount    from t1 order by 5,1;

+------+------------+---------+--------+-----------+--------+

| rank | dense_rank | row_num | userId | channerId | amount |

+------+------------+---------+--------+-----------+--------+

|    1 |          1 |       1 | 王5    | 微信      |    170 |

|    2 |          2 |       2 | 张3    | 微信      |     80 |

|    3 |          3 |       3 | 刘1    | 微信      |     60 |

|    4 |          4 |       4 | 张3    | 微信      |     40 |

|    1 |          1 |       1 | 李4    | 支付宝    |    300 |

|    2 |          2 |       2 | 张3    | 支付宝    |    200 |

|    3 |          3 |       3 | 张3    | 支付宝    |    100 |

|    4 |          4 |       4 | 李4    | 支付宝    |     98 |

|    1 |          1 |       1 | 哈2    | 易支付    |     82 |

|    2 |          2 |       2 | 张3    | 易支付    |     10 |

|    1 |          1 |       1 | 张3    | 易票联    |     60 |

|    1 |          1 |       1 | 田7    | 钱包      |     80 |

|    2 |          2 |       2 | 李4    | 钱包      |     72 |

|    1 |          1 |       1 | 张3    | 银联      |    120 |

|    2 |          2 |       2 | 李4    | 银联      |     40 |

|    1 |          1 |       1 | 赵6    | 闪付      |     20 |

+------+------------+---------+--------+-----------+--------+

16 rows in set (0.01 sec)

MariaDB [test11]>

小结

跟Oracle,PostgreSQL, SQL SERVER还是有差距,不过已经很不错了。希望官方版本也引进这个新特性

MariaDB10.2.X-新特性1-支持分析函数的更多相关文章

  1. Linux环境下Eclipse对C++新特性的支持设置

    Linux环境下Eclipse对C++新特性的支持设置     今天写一个简单的关于C11中的array容器的测试程序如下, #include <iostream> #include &l ...

  2. [置顶] Kubernetes1.7新特性:支持绕过docker,直接通过containerd管理容器

    背景情况 从Docker1.11版本开始,Docker依赖于containerd和runC来管理容器,containerd是控制runC的后台程序,runC是Docker公司按照OCI标准规范编写的一 ...

  3. kubernetes1.4新特性:支持两种新的卷插件

    背景介绍 在Kubernetes中卷的作用在于提供给POD持久化存储,这些持久化存储可以挂载到POD中的容器上,进而给容器提供持久化存储. 从图中可以看到结构体PodSpec有个属性是Volumes, ...

  4. Entity Framework 6 Code First新特性:支持存储过程

    Entity Framework 6提供支持存储过程的新特性,本文具体演示Entity Framework 6 Code First的存储过程操作. Code First的插入/修改/删除存储过程 默 ...

  5. Mozilla新特性只支持https网站,再次推动SSL证书普及

    Mozilla的官方博客2015.4.30正式宣布了淘汰HTTP的方案. 其中包括:设定一个日期,所有的新特性将只提供给HTTPS网站:HTTP网站将逐步被禁止访问浏览器功能,尤其是那些与用户安全和隐 ...

  6. Mozilla新特性只支持https网站

    Mozilla的官方博客2015.4.30正式宣布了淘汰HTTP的方案. 其中包括:设定一个日期,所有的新特性将只提供给HTTPS网站:HTTP网站将逐步被禁止访问浏览器功能,尤其是那些与用户安全和隐 ...

  7. kubernetes1.4新特性:支持sysctl命令

    背景介绍 sysctl是一个允许改变正在运行中的Linux系统内核参数的接口.可以通过sysctl修改Linux系统内核中的TCP/IP 堆栈和虚拟内存系统的高级选项,而且不需要重新启动Linux系统 ...

  8. kubernetes1.4新特性:支持Docker新特性

    (一)背景资料 在Kubernetes1.2中这个第三方组件就是go-dockerclient,这是一个GO语言写的docker客户端,支持Dockerremote API,这个项目在https:// ...

  9. Kubernetes1.3新特性:支持GPU

    (一)  背景资料 GPU就是图形处理器,是Graphics Processing Unit的缩写.电脑显示器上显示的图像,在显示在显示器上之前,要经过一些列处理,这个过程有个专有的名词叫" ...

  10. 启用Servlet 3.0新特性——注解支持

    Servlet 3.0版本新增注解支持,可是在实际使用中,添加的注解总是不起作用.经过检查,原来是“web.xml”文件的顶级标签“<web-app/>”中的一个叫做“metadata-c ...

随机推荐

  1. [ios]ios的延迟执行方法

    1.最直接的方法performSelector:withObject:afterDelay: 这种方法的缺点:每次要为延时写一个方法   2.使用类别,用BOLCK执行 [代码]c#/cpp/oc代码 ...

  2. hash --C++

    题目来源:code[VS] 这是一个极其无聊的hash题.... 1230 元素查找 题目描述 Description 给出n个正整数,然后有m个询问,每个询问一个整数,询问该整数是否在n个正整数中出 ...

  3. phpcms v9后台登陆验证码无法显示,怎么取消验证码

    phpcms v9后台登陆验证码无法显示论坛里关于这个问题貌似一直没有解决,查看源代码后发现,关键一点是获取验证码的图片与全局变量SITE_URL相关,也就是网站的目录, 所以只要修改cache/co ...

  4. Elasticsearch 5.0

    Elasticsearch 5.0 使用ES的基本都会使用过head,但是版本升级到5.0后,head插件就不好使了.下面就看看如何在5.0中启动Head插件吧! 官方粗略教程 Running wit ...

  5. 常用汉字的Unicode码表

    \u96d5\u864e\u7684\u4e00\u4e86\u662f\u6211\u4e0d\u5728\u4eba\u4eec\u6709\u6765\u4ed6\u8fd9\u4e0a\u77 ...

  6. 【转】jquery-取消冒泡

    转自:http://blog.163.com/css_mm/blog/static/209182176201262665157634/ 1.通过返回false来取消默认的行为并阻止事件起泡. jQue ...

  7. windows 2012 服务器打开ping端口,开通远程连接

    windows 2012 服务器打开ping端口,开通远程桌面连接 控制面板->系统与安全->高级防火墙->入站规则 找到:windows 远程桌面 (http in )  右键启用 ...

  8. 【POJ】【2348】Euclid‘s Game

    博弈论 题解:http://blog.sina.com.cn/s/blog_7cb4384d0100qs7f.html 感觉本题关键是要想到[当a-b>b时先手必胜],后面的就只跟奇偶性有关了 ...

  9. 关于ios中的文本操作-简介

    来源:About Text Handling in iOS 官方文档 iOS平台为我们提供了许多在app中展示文本和让用户编辑文本的方式.同时,它也允许你在app视图中展示格式化的文本和网页内容.你可 ...

  10. js String Trim函数

    <javascript> String.prototype.trim = function() { return this.replace(/(^\s*)|(\s*$)/g,"& ...