MySQL implements the ranking syntax like row_numberover in Oracle or PostgreSQL _ MySQL

Source: Internet
Author: User
MySQL implements the ranking syntax such as Oracle or PostgreSQL row_numberover bitsCN. comPostgreSQL and Oracle both provide statements such as row_number () over () to rank corresponding fields, which is very convenient. MySQL does not provide such a syntax.
Table "ytt. t1" Column | Type | Modifiers -------- + ----------------------- + ----------- I _name | character varying (10) | not null rank | integer | not null
t_girl=# select * from t1 order by i_name; i_name | rank ---------+------ Charlie | 12 Charlie | 12 Charlie | 13 Charlie | 10 Charlie | 11 Lily | 6 Lily | 7 Lily | 7 Lily | 6 Lily | 5 Lily | 7 Lily | 4 Lucy | 1 Lucy | 2 Lucy | 2 Ytt | 14 Ytt | 15 Ytt | 14 Ytt | 14 Ytt | 15 (20 rows)
T_girl = # select I _name, rank, row_number () over (partition by I _name order by rank desc) as rank_number from t1; I _name | rank | rank_number --------- + ------ + ------------- Charlie | 13 | 1 Charlie | 12 | 2 Charlie | 12 | 3 Charlie | 11 | 4 Charlie | 10 | 5 Lily | 7 | 1 lily | 7 | 2 Lily | 7 | 3 Lily | 6 | 4 Lily | 6 | 5 Lily | 5 | 6 Lily | 4 | 7 Lucy | 2 | 1 Lucy | 2 | 2 lucy | 1 | 3 Ytt | 15 | 1 Ytt | 15 | 2 Ytt | 14 | 3 Ytt | 14 | 4 Ytt | 14 | 5 (20 rows)
 


t_girl=# select i_name,rank, row_number() over(partition by i_name ) as rank_number from t1; i_name  | rank | rank_number ---------+------+------------- Charlie |   12 |           1 Charlie|   12 |           2 Charlie|   13 |           3 Charlie|   10 |           4 Charlie|   11 |           5 Lily|    6 |           1 Lily|    7 |           2 Lily|    7 |           3 Lily|    6 |           4 Lily|    5 |           5 Lily|    7 |           6 Lily|    4 |           7 Lucy|    1 |           1 Lucy|    2 |           2 Lucy|    2 |           3 Ytt|   14 |           1 Ytt|   15 |           2 Ytt|   14 |           3 Ytt|   14 |           4 Ytt|   15 |           5(20 rows)



t_girl=# select i_name,rank, row_number() over() as rank_number from t1; i_name  | rank | rank_number ---------+------+------------- Lily |    7 |           1 Lucy|    2 |           2 Ytt|   14 |           3 Ytt|   14 |           4 Charlie|   12 |           5 Charlie|   13 |           6 Lily|    7 |           7 Lily|    4 |           8 Ytt|   14 |           9 Lily|    6 |          10 Lucy|    1 |          11 Lily|    7 |          12 Ytt|   15 |          13 Lily|    6 |          14 Charlie|   11 |          15 Charlie|   12 |          16 Lucy|    2 |          17 Charlie|   10 |          18 Lily|    5 |          19 Ytt|   15 |          20(20 rows)


DELIMITER $ USE't _ girl $ drop procedure if exists 'sp _ rownumber' $ create procedure 'sp _ rownumber' (IN f_table_name VARCHAR (64 ), IN f_column_partitionby VARCHAR (64), IN f_column_orderby VARCHAR (64), IN f_is_asc CHAR (4) BEGIN -- Created by ytt at 2014/1/10 -- Do a row_number () over () DECLARE I INT; -- Create a temporary table to save result. drop table if exists tmp_rownum; SET @ stmt = CONCAT ('create temporary table tmp_rownum select *, ''rownum'' from ', f_table_name, 'where 1 = 0 '); PREPARE s1 FROM @ stmt; EXECUTE s1; SET I = 0; SET @ j = 0; SET @ v_column_paritionby = ''; -- Check whether parition column is null or not. IF (f_column_partitionby = ''OR f_column_partitionby is null) THEN -- No additional parition column. SET @ stmt = CONCAT ('Insert into tmp_rownum select *, @ j: = @ j + 1 as rownum from ', f_table_name); PREPARE s1 FROM @ stmt; EXECUTE s1; ELSE -- Give partition column. SET @ stmt = CONCAT ('SELECT count (*) from (select count (*) from ', f_table_name, 'group by', f_column_partitionby ,') as a into @ cnt '); PREPARE s1 FROM @ stmt; EXECUTE s1; WHILE I <@ cnt DO -- Get the partition value one by one. SET @ stmt = CONCAT ('select', f_column_partitionby, 'from', f_table_name, 'group by', f_column_partitionby, 'limit', I, ', 1 into @ v_column_partitionby '); PREPARE s1 FROM @ stmt; EXECUTE s1; -- Check whether sort is needed. IF f_column_orderby = ''OR f_column_orderby is null then set @ stmt = CONCAT ('Insert into tmp_rownum select *, @ j: = @ j + 1 as rownum from ', f_table_name, 'where', f_column_partitionby, '= ''', @ v_column_partitionby, '''); else set @ stmt = CONCAT ('Insert into tmp_rownum select *, @ j: = @ j + 1 as rownum from ', f_table_name, 'where', f_column_partitionby,' = ''', @ v_column_partitionby, '''order by', f_column_orderby ,'', f_is_asc); end if; SET @ j = 0; PREPARE s1 FROM @ stmt; EXECUTE s1; SET I = I + 1; END WHILE; END IF; -- Reset all session variables. SET @ j = NULL; SET @ v_column_paritionby = NULL; SET @ cnt = NULL; SELECT * FROM tmp_rownum; END $ DELIMITER;
 


CALL sp_rownumber('t1','i_name','rank','desc');query resulti_namerankrownumCharlie131Charlie122Charlie123Charlie114Charlie105Lily71Lily72Lily73Lily64Lily65Lily56Lily47Lucy21Lucy22Lucy13Ytt151Ytt152Ytt143Ytt144Ytt145

Second,

CALL sp_rownumber('t1','i_name',NULL,NULL);query resulti_namerankrownumCharlie121Charlie132Charlie113Charlie124Charlie105Lily71Lily72Lily43Lily64Lily75Lily66Lily57Lucy21Lucy12Lucy23Ytt141Ytt142Ytt143Ytt154Ytt155

Third,

CALL sp_rownumber('t1',NULL,NULL,NULL);query resulti_namerankrownumLily71Lucy22Ytt143Ytt144Charlie125Charlie136Lily77Lily48Ytt149Lily610Lucy111Lily712Ytt1513Lily614Charlie1115Charlie1216Lucy217Charlie1018Lily519Ytt1520


BitsCN.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.