我的编程空间,编程开发者的网络收藏夹
学习永远不晚

Mysql似oracle分析函数sum over的实现方法是什么

短信预约 -IT技能 免费直播动态提醒
省份

北京

  • 北京
  • 上海
  • 天津
  • 重庆
  • 河北
  • 山东
  • 辽宁
  • 黑龙江
  • 吉林
  • 甘肃
  • 青海
  • 河南
  • 江苏
  • 湖北
  • 湖南
  • 江西
  • 浙江
  • 广东
  • 云南
  • 福建
  • 海南
  • 山西
  • 四川
  • 陕西
  • 贵州
  • 安徽
  • 广西
  • 内蒙
  • 西藏
  • 新疆
  • 宁夏
  • 兵团
手机号立即预约

请填写图片验证码后获取短信验证码

看不清楚,换张图片

免费获取短信验证码

Mysql似oracle分析函数sum over的实现方法是什么

这篇文章主要介绍“Mysql似oracle分析函数sum over的实现方法是什么”,在日常操作中,相信很多人在Mysql似oracle分析函数sum over的实现方法是什么问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Mysql似oracle分析函数sum over的实现方法是什么”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

先看oracle怎么实现的

  1. select deptno,ename,sal,sum(sal) over(order by ename) from emp; --姓名排序连续求和

  2. select deptno,ename,sal,sum(sal) over(order by deptno) from emp; --所有部们排序连续求和

  3. select deptno,ename,sal,sum(sal) over(partition by deptno) from emp; ---各个部门的总和

  4. select deptno,ename,sal,sum(sal) over(partition by deptno order by ename) from emp; ---各个部门之间连续求和

  5. select deptno,ename,sal,sum(sal) over(order by deptno,ename) from emp;



  6. select deptno,ename,sal,

  7.       sum(sal) over (partition by deptno order by ename) 部门连续求和,--各部门的薪水"连续"求和

  8.       sum(sal) over (partition by deptno) 部门总和, -- 部门统计的总和,同一部门总和不变

  9.       100*round(sal/sum(sal) over (partition by deptno),4) "部门份额(%)",

  10.       sum(sal) over (order by deptno, ename) 连续求和, --所有部门的薪水"连续"求和

  11.       sum(sal) over () 总和, -- 此处sum(sal) over () 等同于sum(sal),所有员工的薪水总和

  12.      100*round(sal/sum(sal) over (),4) "总份额(%)"

  13.      from emp



mysql的实现

  1. 如下:

  2. SELECT a.id,a.user_id,a.borrow_id, a.repayment_money,
    (SELECT SUM(repayment_money) FROM rb_repayment_period WHERE id<=a.id) "累加和",

  3. (SELECT AVG(repayment_money) FROM rb_repayment_period WHERE id<=a.id) "平均值" ,
    (SELECT SUM(repayment_money) FROM rb_repayment_period  WHERE borrow_id=a.borrow_id GROUP BY borrow_id) "每组和",
    (SELECT SUM(repayment_money) FROM rb_repayment_period) "全部和",
    (SELECT SUM(repayment_money) FROM rb_repayment_period  WHERE id<=a.id  GROUP BY borrow_id HAVING  borrow_id=a.`borrow_id` ) "每组累加和"    
    FROM rb_repayment_period a;

结果
Mysql似oracle分析函数sum over的实现方法是什么

原数据

  1. sql:

  2. CREATE TABLE `rb_repayment_period` (
      `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
      `borrow_id` int(11) DEFAULT '0' COMMENT '标的id',
      `user_id` int(11) DEFAULT '0' COMMENT '借款人id',
      `repayment_money` decimal(20,6) DEFAULT '0.000000' COMMENT '本次还款金额',
      `capital_money` decimal(20,6) DEFAULT '0.000000' COMMENT '本金',
      `expect_money` decimal(20,6) DEFAULT '0.000000' COMMENT '预期收益',
      `exceed_money` decimal(20,6) DEFAULT '0.000000' COMMENT '超额收益',
      `actual_rate` decimal(20,6) DEFAULT '0.000000' COMMENT '实际收益率',
      `third_company_money` decimal(20,6) DEFAULT '0.000000' COMMENT '第三方公司收益',
      `load_money` decimal(20,6) DEFAULT '0.000000' COMMENT '借款人利益',
      `repayment_time` int(3) DEFAULT '0' COMMENT '还款次数',
      `repayment_stage` int(3) DEFAULT '0' COMMENT '当前还款的阶段',
      `playform_money` decimal(20,6) DEFAULT '0.000000' COMMENT '平台收益',
      `add_datetime` timestamp NOT NULL DEFAULT '2016-04-24 03:49:30' COMMENT '操作时间',
      `memo_id_first` int(11) DEFAULT '0' COMMENT '备用id',
      `memo_dec_first` decimal(20,6) DEFAULT '0.000000' COMMENT '备用dec',
      `memo_str_first` varchar(500) DEFAULT NULL COMMENT '备用str1',
      `memo_str_second` varchar(500) DEFAULT NULL COMMENT '备用str2',
      `memo_date_first` timestamp NULL DEFAULT '2016-04-24 03:49:30' COMMENT '备用时间1',
      `memo_date_second` timestamp NULL DEFAULT '2016-04-24 03:49:30' COMMENT '备用时间2',
      `total_repay_money` decimal(20,6) DEFAULT '0.000000' COMMENT '累计还款总额',
      `repay_type` int(3) DEFAULT '0' COMMENT '还款类型',
      `left_capital_money` decimal(20,6) DEFAULT '0.000000' COMMENT '剩余本金',
      `left_expect_money` decimal(20,6) DEFAULT '0.000000' COMMENT '剩余收益',
      `left_money` decimal(20,6) DEFAULT '0.000000' COMMENT '剩余留用',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=60 DEFAULT CHARSET=utf8;
    ;


    --
    -- Dumping data for table `rb_repayment_period`
    --


    LOCK TABLES `rb_repayment_period` WRITE;
    ;
    INSERT INTO `rb_repayment_period` VALUES (26,160,188,1000.000000,1000.000000,0.000000,0.000000,0.000000,0.000000,0.000000,1,2,0.0000
    00,'2016-04-24 07:43:38',0,0.000000,NULL,NULL,'2016-04-24 03:49:30','2016-04-24 03:49:30',0.000000,0,0.000000,0.000000,0.000000),(27
    ,160,188,100.000000,0.000000,100.000000,0.000000,0.000000,0.000000,0.000000,2,2,0.000000,'2016-04-24 07:45:26',0,0.000000,NULL,NULL,
    '2016-04-24 03:49:30','2016-04-24 03:49:30',0.000000,0,0.000000,0.000000,0.000000),(30,160,188,1000.000000,0.000000,87.500000,11.250
    000,0.000000,11.250000,890.000000,3,4,0.000000,'2016-04-24 08:09:11',0,0.000000,NULL,NULL,'2016-04-24 03:49:30','2016-04-24 03:49:30
    ',0.000000,0,0.000000,0.000000,0.000000),(42,163,187,4400.000000,2000.000000,375.000000,0.000000,0.000000,0.000000,2025.000000,1,3,0
    .000000,'2016-04-25 07:33:59',0,0.000000,NULL,NULL,'2016-04-25 07:33:59','2016-04-25 07:33:59',0.000000,0,0.000000,0.000000,0.000000
    ),(47,172,187,10000.000000,2000.000000,375.000000,12.500000,0.000000,12.500000,7600.000000,1,4,0.000000,'2016-04-26 02:48:05',0,0.00
    0000,NULL,NULL,'2016-04-26 02:48:05','2016-04-26 02:48:05',0.000000,0,0.000000,0.000000,0.000000),(48,174,187,10000.000000,2000.0000
    00,375.000000,12.500000,0.000000,12.500000,7600.000000,1,4,0.000000,'2016-04-26 03:23:41',0,0.000000,NULL,NULL,'2016-04-26 03:23:41'
    ,'2016-04-26 03:23:41',0.000000,0,0.000000,0.000000,0.000000),(49,157,187,3000.000000,1000.000000,120.000000,0.000000,0.000000,0.000
    000,1880.000000,1,3,0.000000,'2016-04-26 03:58:56',0,0.000000,NULL,NULL,'2016-04-26 03:58:56','2016-04-26 03:58:56',3000.000000,2,0.
    000000,0.000000,0.000000),(50,175,187,10000.000000,2000.000000,375.000000,12.500000,0.000000,12.500000,7600.000000,1,4,0.000000,'201
    6-04-26 05:29:48',0,0.000000,NULL,NULL,'2016-04-26 05:29:48','2016-04-26 05:29:48',10000.000000,2,0.000000,0.000000,0.000000),(54,17
    7,187,2000.000000,2000.000000,0.000000,0.000000,0.000000,0.000000,0.000000,1,2,0.000000,'2016-04-27 01:59:35',0,0.000000,NULL,NULL,'
    2016-04-27 01:59:35','2016-04-27 01:59:35',2000.000000,1,0.000000,375.000000,0.000000),(55,177,187,4000.000000,0.000000,375.000000,0
    .000000,360.000000,0.000000,3625.000000,2,3,0.000000,'2016-04-27 02:01:43',0,0.000000,NULL,NULL,'2016-04-27 02:01:43','2016-04-27 02
    :01:43',6000.000000,2,0.000000,0.000000,0.000000),(56,178,187,2100.000000,2000.000000,100.000000,0.000000,0.000000,0.000000,0.000000
    ,1,2,0.000000,'2016-04-27 03:43:43',0,0.000000,NULL,NULL,'2016-04-27 03:43:43','2016-04-27 03:43:43',2100.000000,1,0.000000,275.0000
    00,0.000000),(57,178,187,3000.000000,0.000000,275.000000,0.000000,378.000000,0.000000,2725.000000,2,3,0.000000,'2016-04-27 07:07:34'
    ,0,0.000000,NULL,NULL,'2016-04-27 07:07:34','2016-04-27 07:07:34',5100.000000,2,0.000000,0.000000,0.000000),(58,181,187,1000.000000,
    1000.000000,0.000000,0.000000,0.000000,0.000000,0.000000,1,1,0.000000,'2016-04-27 07:15:58',0,0.000000,NULL,NULL,'2016-04-27 07:15:5
    8','2016-04-27 07:15:58',1000.000000,1,1000.000000,375.000000,0.000000),(59,181,187,500.000000,500.000000,0.000000,0.000000,180.0000
    00,0.000000,0.000000,2,1,0.000000,'2016-04-27 07:26:34',0,0.000000,NULL,NULL,'2016-04-27 07:26:34','2016-04-27 07:26:34',1500.000000
    ,1,500.000000,375.000000,0.000000);

rownum的实现

  1. 环境:

  2. mysql> show create table tbl\G;
    *************************** 1. row ***************************
           Table: tbl
    Create Table: CREATE TABLE `tbl` (
      `id` int(11) NOT NULL,
      `col` int(11) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8

  3. mysql> insert into tbl values (1,26),(2,46),(3,35),(4,68),(5,93),(6,92);

  4. mysql> select * from tbl
        -> ;
    +----+------+
    | id | col  |
    +----+------+
    |  1 |   26 |
    |  2 |   46 |
    |  3 |   35 |
    |  4 |   68 |
    |  5 |   93 |
    |  6 |   92 |
    +----+------+
    6 rows in set (0.00 sec)



  5. 实现一:





  6. 实现二:解决重复bug(先建立一张数字表Nums(a int) 插入1-100即可


    第二步:
    MySQL [interface_hd_com]> select Nums.a+c.rownum as rank ,col from (select a.col,COUNT(*) as count,( select count(*) from testtt b where b.col<a.col) as rownum from testtt a group by a.col) c,Nums where Nums.a<=count order by col;
    +------+------+
    | rank | col  |
    +------+------+
    |    1 |   26 |
    |    2 |   35 |
    |    3 |   35 |
    |    4 |   46 |
    |    5 |   46 |
    |    6 |   68 |
    |    7 |   68 |
    |    8 |   92 |
    |    9 |   92 |
    |   10 |   93 |
    |   11 |   93 |
    +------+------+
    11 rows in set (0.01 sec)






    连续区间的实现(求连续id区间)

    第二步:计算一下与标示的差值(如果是连续的,那么差值一样)
    mysql> SELECT id,alias1,(id-alias1) AS diff FROM (SELECT id,@id:=@id+1  AS alias1 FROM tbl,(SELECT @id:=0) AS id) b;
    +----+--------+------+
    | id | alias1 | diff |
    +----+--------+------+
    | 11 |      1 |   10 |
    | 12 |      2 |   10 |
    | 13 |      3 |   10 |
    | 14 |      4 |   10 |
    | 15 |      5 |   10 |
    | 16 |      6 |   10 |
    | 18 |      7 |   11 |
    | 19 |      8 |   11 |
    +----+--------+------+
    8 rows in set (0.00 sec)


    第三步:根据差值分组找出最大最小即可
    mysql> SELECT MIN(id) start_pos,MAX(id) end_pos
        -> FROM
        -> (SELECT id,alias1,(id-alias1) AS diff FROM (SELECT id,@id:=@id+1  AS alias1 FROM tbl,(SELECT @id:=0) AS id) b)
        -> AS c
        -> GROUP BY diff;
    +-----------+---------+
    | start_pos | end_pos |
    +-----------+---------+
    |        11 |      16 |
    |        18 |      19 |
    +-----------+---------+
    2 rows in set (0.00 sec)



    实验:求tel相同的连续段

    按照上面的思路求得
    MySQL [interface_hd_com]> SELECT MIN(id) start_pos,MAX(id) end_pos,tel FROM (SELECT id,alias1,(id-alias1) AS diff,tel FROM (SELECT id,@id:=@id+1  AS alias1,tel FROM testtab,(SELECT @id:=0) AS id) b) as c GROUP BY diff,tel order by tel desc;
    +-----------+---------+--------+
    | start_pos | end_pos | tel    |
    +-----------+---------+--------+
    |         3 |       7 | 187164 |
    |         1 |       8 | 187163 |
    |         9 |       9 |  19999 |
    +-----------+---------+--------+   ---这样是有bug的

    发现这样是不行的,因为id是连续的,所以同一个tel的diff是相同的,但其实中间隔着别的tel
    解决办法:分两次求在合并

    union 一下

    1. MySQL [interface_hd_com]> SELECT MIN(id) start_pos,MAX(id) end_pos,tel FROM (SELECT id,alias1,(id-alias1) AS diff,tel FROM (SELECT id,@id:=@id+1  AS alias1,tel FROM testtab,(SELECT @id:=0) AS id where tel in (SELECT distinct(tel) from testtab where tel<>187164)) b) as c GROUP BY diff,tel order by tel desc;

    2. +-----------+---------+--------+

    3. | start_pos | end_pos | tel |

    4. +-----------+---------+--------+

    5. | 1 | 2 | 187163 |

    6. | 5 | 6 | 187163 |

    7. | 8 | 8 | 187163 |

    8. | 9 | 9 | 19999 |

    9. +-----------+---------+--------+

    10. 4 rows in set (0.00 sec)


    11. MySQL [interface_hd_com]> SELECT MIN(id) start_pos,MAX(id) end_pos,tel FROM (SELECT id,alias1,(id-alias1) AS diff,tel FROM (SELECT id,@id:=@id+1  AS alias1,tel FROM testtab,(SELECT @id:=0) AS id where tel in (187164)) b) as c GROUP BY diff,tel order by tel desc;

    12. +-----------+---------+--------+

    13. | start_pos | end_pos | tel |

    14. +-----------+---------+--------+

    15. | 3 | 4 | 187164 |

    16. | 7 | 7 | 187164 |

    17. +-----------+---------+--------+

    18. 2 rows in set (0.00 sec)

    19. MySQL [interface_hd_com]> select * from testtab;

    20. +------+--------+

    21. | id | tel |

    22. +------+--------+

    23. | 1 | 187163 |

    24. | 2 | 187163 |

    25. | 3 | 187164 |

    26. | 4 | 187164 |

    27. | 5 | 187163 |

    28. | 6 | 187163 |

    29. | 7 | 187164 |

    30. | 8 | 187163 |

    31. | 9 | 19999 |

    32. +------+--------+

    33. 9 rows in set (0.00 sec)

    34. 第一步:标示

    35. mysql> SELECT id,@id:=@id+1  AS alias1 FROM tbl,(SELECT @id:=0) AS id;
      +----+--------+
      | id | alias1 |
      +----+--------+
      | 11 |      1 |
      | 12 |      2 |
      | 13 |      3 |
      | 14 |      4 |
      | 15 |      5 |
      | 16 |      6 |
      | 18 |      7 |
      | 19 |      8 |
      +----+--------+
      8 rows in set (0.00 sec)

    36. 第一步求出个数

    37. MySQL [interface_hd_com]> select a.col,COUNT(*) as count,( select count(*) from testtt b where b.col<a.col) as rownum from testtt a group by a.col;

    38. +------+-------+--------+

    39. | col | count | rownum |

    40. +------+-------+--------+

    41. | 26 | 1 | 0 |

    42. | 35 | 2 | 1 |

    43. | 46 | 2 | 3 |

    44. | 68 | 2 | 5 |

    45. | 92 | 2 | 7 |

    46. | 93 | 2 | 9 |

    47. +------+-------+--------+

    48. 6 rows in set (0.00 sec)

    49. mysql> select id,a.col,( select count(*) from tbl b where b.col<=a.col) as rank from tbl a order by rank;
      +----+------+------+
      | id | col  | rank |
      +----+------+------+
      |  1 |   26 |    1 |
      |  3 |   35 |    2 |
      |  2 |   46 |    3 |
      |  4 |   68 |    4 |
      |  6 |   92 |    5 |
      |  5 |   93 |    6 |
      +----+------+------+
      6 rows in set (0.00 sec)

    50. 瑕疵:当有重复的数据时就有bug了

    51. mysql> select id,a.col,(select count(*) from tbl b where b.col<=a.col ) as rank from tbl a order by rank;
      +----+------+------+
      | id | col  | rank |
      +----+------+------+
      |  1 |   26 |    2 |
      |  9 |   26 |    2 |
      |  3 |   35 |    4 |
      |  8 |   35 |    4 |
      |  2 |   46 |    5 |
      |  4 |   68 |    6 |
      |  6 |   92 |    7 |
      |  5 |   93 |    8 |
      +----+------+------+
      8 rows in set (0.00 sec)



到此,关于“Mysql似oracle分析函数sum over的实现方法是什么”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

Mysql似oracle分析函数sum over的实现方法是什么

下载Word文档到电脑,方便收藏和打印~

下载Word文档

猜你喜欢

MySQL实现数据分页的方法是什么

MySQL实现数据分页的方法通常是使用LIMIT子句。通过在查询语句中使用LIMIT子句,可以限制返回的数据行数,并可以指定返回数据的起始位置。例如,以下是一个基本的数据分页查询语句示例:SELECT * FROM table_name
MySQL实现数据分页的方法是什么
2024-04-09

mysql分割函数的使用方法是什么

在MySQL中,SUBSTRING函数用于从一个字符串中提取子字符串。其基本语法如下:SUBSTRING(str, start, length)其中:str为要提取子字符串的原始字符串start为要提取子字符串的起始位置(从1开始计数)
mysql分割函数的使用方法是什么
2024-04-09

r语言数据分析的实现方法是什么

R语言是一种功能强大的编程语言和环境,特别适用于数据分析。以下是R语言实现数据分析的一般方法:1. 数据导入:使用R语言中的函数从各种数据源(如CSV、Excel、数据库)中导入数据集,并将其存储在R中的数据结构(如数据框)中。2. 数据清
2023-09-15

mysql limit分页实现的方法是什么

MySQL中的LIMIT语句用于限制查询结果的数量。它的基本语法如下:SELECT column1, column2, ...FROM table_nameLIMIT offset, count;其中,offset表示查询结果的起始位
mysql limit分页实现的方法是什么
2024-04-09

mysql读写分离实现的方法是什么

MySQL读写分离是通过将读操作和写操作分别分配到不同的MySQL实例上来实现的。常见的实现方法有以下两种:通过MySQL代理服务器实现读写分离:在MySQL代理服务器上配置一个读写分离规则,根据SQL语句的类型将读操作和写操作分别转发到
mysql读写分离实现的方法是什么
2024-04-09

PHP加盐实现函数的方法是什么

今天小编给大家分享一下PHP加盐实现函数的方法是什么的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。第一步:生成一个随机的盐值
2023-07-05

mysql分布式锁实现的方法是什么

MySQL本身并没有提供分布式锁的实现方法,但可以借助MySQL的特性和其他技术来实现分布式锁。以下是几种常见的实现方法:1. 基于数据库的乐观锁:在表中添加一个version字段,通过比较version值来判断是否可以获取锁。每次更新前先
2023-10-09

c++类函数作为模板参数实现的方法是什么

今天小编给大家分享一下c++类函数作为模板参数实现的方法是什么的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。需求背景DB操作
2023-07-05

MySQL对数据表已有表进行分区表的实现方法是什么

本篇内容介绍了“MySQL对数据表已有表进行分区表的实现方法是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!操作方式@1 可以使用ALT
2023-06-25

JS中call、apply和bind函数手写实现demo的方法是什么

本篇内容介绍了“JS中call、apply和bind函数手写实现demo的方法是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!JavaS
2023-07-05

Python中的魔法函数与量子计算模拟实现的方法是什么

这篇“Python中的魔法函数与量子计算模拟实现的方法是什么”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Python中的魔
2023-07-05

C语言实现猜数字游戏的两种方法分别是什么

C语言实现猜数字游戏的两种方法分别是什么,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。前言猜数字小游戏是我们大多数人学习C语言时都会了解到的一个有趣的C语言小游戏,下面我将详细
2023-06-26

MySQL存储过程创建使用及实现数据快速插入的方法是什么

本文小编为大家详细介绍“MySQL存储过程创建使用及实现数据快速插入的方法是什么”,内容详细,步骤清晰,细节处理妥当,希望这篇“MySQL存储过程创建使用及实现数据快速插入的方法是什么”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一
2023-03-10

R语言数据可视化tidyr与ggplot2多个变量分层展示的实现方法是什么

本篇内容主要讲解“R语言数据可视化tidyr与ggplot2多个变量分层展示的实现方法是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“R语言数据可视化tidyr与ggplot2多个变量分层展
2023-06-25

编程热搜

目录