91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Mysql似oracle分析函數sum over的實現方法是什么

發布時間:2021-11-18 17:10:00 來源:億速云 閱讀:165 作者:iii 欄目:MySQL數據庫

這篇文章主要介紹“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;
    /*!40101 SET character_set_client = @saved_cs_client */;


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


    LOCK TABLES `rb_repayment_period` WRITE;
    /*!40000 ALTER TABLE `rb_repayment_period` DISABLE KEYS */;
    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的實現方法是什么”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

自贡市| 五原县| 丹凤县| 五莲县| 奇台县| 朔州市| 金湖县| 江油市| 洞口县| 内江市| 柘城县| 宁陵县| 黄山市| 淮安市| 贡嘎县| 平和县| 宝坻区| 玉林市| 明星| 巴东县| 望江县| 旬邑县| 江门市| 盘山县| 阿坝县| 宜兰县| 梁山县| 广东省| 泸西县| 岗巴县| 富裕县| 封丘县| 绥阳县| 资兴市| 永胜县| 宜宾县| 丁青县| 上蔡县| 鄄城县| 满洲里市| 塔城市|