MySQL Foundation - 1 - row_number () over (partition by expr1, expr2... order by expr1)

Keywords: MySQL SQL

In the project, you see that the designer provides an sql question, using row_number() and over() functions. I don't even understand MySQL, so it's a bit too difficult for me.
I'll record the replacement of the function of the question, and I don't know if anyone can use it.

demand


Once processed by functions on the title, the original table can be grouped according to uid and JAN, and a rownum can be generated in each group. It's troublesome to say, but actually dba wrote this code

select
  JAN,
  uid,
  trade_date,
  row_number() over ( partition by uid,JAN order by trade_date  ) as no  
from base

He might be SqlServer, and that's what it should be.

MySQL transformation

1. Preparing data

DROP TABLE IF EXISTS `base`;
CREATE TABLE `base` (
  `trade_date` varchar(255) DEFAULT NULL,
  `uid` varchar(255) DEFAULT NULL,
  `oid` text,
  `product_name` varchar(255) DEFAULT NULL,
  `product_kind` text,
  `JAN` varchar(255) DEFAULT NULL,
  `detail_turnover` varchar(255) DEFAULT NULL,
  `turnover_count` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of base
-- ----------------------------
INSERT INTO `base` VALUES ('20170914', 'LIUYANG', '294000813967900126101', 'AAA', 'Consumption of household appliancesけ', '1111', '3602', '1');
INSERT INTO `base` VALUES ('20170915', 'LIUYANG', '294000813967900126101', 'AAA', 'Consumption of household appliancesけ', '1111', '3602', '1');
INSERT INTO `base` VALUES ('20170925', 'LIUYANG', '294000813967900126101', 'AAA', 'Consumption of household appliancesけ', '1111', '3602', '1');
INSERT INTO `base` VALUES ('20170914', 'LIUYANG', '294000813967900126101', 'BBB', 'Set upきclockリビング', '2222', '3602', '1');
INSERT INTO `base` VALUES ('20170915', 'LIUYANG', '294000813967900126101', 'BBB', 'Set upきclockリビング', '2222', '3602', '1');
INSERT INTO `base` VALUES ('20170925', 'LIUYANG', '294000813967900126101', 'BBB', 'Set upきclockリビング', '2222', '3602', '1');
INSERT INTO `base` VALUES ('20170920', 'MOROYA', '294000813967900126101', 'AAA', 'Consumption of household appliancesけ', '1111', '3602', '1');
INSERT INTO `base` VALUES ('20170912', 'MOROYA', '294000813967900126101', 'AAA', 'Consumption of household appliancesけ', '1111', '3602', '1');
INSERT INTO `base` VALUES ('20170911', 'MOROYA', '294000813967900126101', 'AAA', 'Consumption of household appliancesけ', '1111', '3602', '1');
INSERT INTO `base` VALUES ('20170911', 'MOROYA', '294000813967900126101', 'BBB', 'Set upきclockリビング', '2222', '3602', '1');
INSERT INTO `base` VALUES ('20170912', 'MOROYA', '294000813967900126101', 'BBB', 'Set upきclockリビング', '2222', '3602', '1');
INSERT INTO `base` VALUES ('20170925', 'MOROYA', '294000813967900126101', 'BBB', 'Set upきclockリビング', '2222', '3602', '1');

2. Direct to the final result

select JAN,uid,trade_date,no from (  
    select 
    base_tmp.JAN,base_tmp.uid,base_tmp.trade_date,@rownum:=@rownum+1,
    if(@pjan=base_tmp.JAN and @puid=base_tmp.uid, @rank:=@rank+1,@rank:=1) as no,
    @puid:=base_tmp.uid,
    @pjan:=base_tmp.JAN 
    from (select JAN,uid,trade_date from base  
            order by uid,JAN,trade_date asc) base_tmp ,
         (select  @rownum :=0 , @puid := null ,@pjan:=null ,@rank:=0) a ) result  

3. Keep a brief record of what you think now.

Since I don't understand MySQL, I'd like to express my thoughts briefly.

  1. First, we need to have a temp table of partition by and order by in the title function, so we have a base_tmp.
  2. At the same time, I need to initialize a few parameters, at least that's what I understand.
  3. The largest sub-query defines two parameters, JAN and UID, which are also the two fields we want to group. I paste the results of the largest sub-query for a record.

    A brief explanation
    The field @rownum: Since I was running for the second time, I saw that her value did not start from 1, because the sql I selected did not include the query initialized for the parameter at last.
    At the same time, two parameters are defined. Through the if function, when these two parameters are the same as the queried data, we increase ourselves. Otherwise, the rownum of sorting is realized by counting them again from the beginning.

Posted by freynolds on Wed, 19 Dec 2018 18:45:05 -0800