Row number per group in mysql

前端 未结 6 530
臣服心动
臣服心动 2020-11-27 17:27

I want to produce query result base on this scenario that can create row number according to crew_id and type.

id   crew_id   amount    type
 1      4                


        
相关标签:
6条回答
  • 2020-11-27 17:51

    Here is my answer using only one case that creates row number according to both columns:

    SELECT id, crew_id, amount, type,
       (CASE CONCAT(crew_id, type) 
            WHEN @cur_crew_type
            THEN @curRow := @curRow + 1
            ELSE @curRow := 0 END) + 1 AS cnt,
        @cur_crew_type := CONCAT(crew_id, type) AS cur_crew_type
    FROM TABLE t,
         (SELECT @curRow := 0, @cur_crew_type := '') counter
          ORDER BY crew_id, type;
    
    0 讨论(0)
  • 2020-11-27 17:58

    Please go through my fiddle

    This One Last Tried

        SELECT    id,
                  crew_id,
                  amount,
                  type,
                 ( 
                    CASE type 
                    WHEN @curType 
                    THEN @curRow := @curRow + 1 
                    ELSE @curRow := 1 AND @curType := type END
                  ) + 1 AS rank
        FROM      Table1 p,
                  (SELECT @curRow := 0, @curType := '') r
       ORDER BY  crew_id,type asc;
    
    0 讨论(0)
  • 2020-11-27 18:02
    SELECT id, crew_id, amount, type,
           ( 
            CASE type 
              WHEN @curType 
              THEN @curRow := @curRow + 1 
              ELSE @curRow := 1 AND @curType := type  END
          ) + 1 AS rank
     FROM      Table1 p,
          (SELECT @curRow := 0, @curType := '') r
       ORDER BY  crew_id, type asc;
    
    0 讨论(0)
  • 2020-11-27 18:10

    @Janty's answer does not work when the type starts with a number (the rank starts at 2 instead of 1).

    Use the following instead:

    SELECT id,
         crew_id,
         amount,
         CASE type 
             WHEN @curType THEN @curRow := @curRow + 1 
             ELSE @curRow := 1
         END AS rank,
         @curType := type AS type
    FROM Table1 p
    JOIN (SELECT @curRow := 0, @curType := '') r
    ORDER BY crew_id, type
    
    0 讨论(0)
  • 2020-11-27 18:12

    In addition to the answer of @Janty here is a solution if you want to UDATE your table with the rownumber:

    UPDATE myTable mt,(SELECT @curRow := 0, @curType := '') r SET type=
        ( 
            CASE type 
                WHEN @curType
            THEN @curRow := @curRow + 1 
            ELSE @curRow := 1 AND @curType := type END
          )
    ;
    

    As janty too crewId is not within. Use a second "case" for that as mentioned in the other answers.

    0 讨论(0)
  • 2020-11-27 18:15

    The question is quite old. But I would like to post it in case someone will have a same problem.

    First of all, described answers do not work correct. For example, for

    id   crew_id   amount    type
    1      4       1000      AUB
    2      4       1500      AUB
    5      5       1000      AUB
    6      6       3000      AUB
    8      6       3500      AUB
    9      4       5000      AUB
    

    (I just removed rows with type 'CA') the result table will be

    id   crew_id   amount    rank   type
    1      4       1000      1      AUB
    2      4       1500      2      AUB
    9      4       5000      3      AUB
    5      5       1000      4      AUB
    6      6       3000      5      AUB
    8      6       3500      6      AUB
    

    So in fact it doesn't use both crew_id and type, it just uses type.

    Here is how I solved this problem (probably there is a more elegant way to do it than use two nested 'CASE's, but you get the idea):

    SELECT id,
        amount,
        CASE crew_id 
            WHEN @curCrewId THEN
                CASE type 
                    WHEN @curType THEN @curRow := @curRow + 1 
                    ELSE @curRow := 1
                END
            ELSE @curRow :=1
        END AS rank,
        @curCrewId := crew_id AS crew_id,
        @curType := type AS type
    FROM Table1 p
    JOIN (SELECT @curRow := 0, @curCrewId := 0, @curType := '') r
    ORDER BY crew_id, type
    

    The main idea remain. I just added a variable @curCrewId. If someone need to use 3 variables for grouping, so just use 3 variables and 3 nested 'CASE's. :)

    0 讨论(0)
提交回复
热议问题