Multiple Ranks in one table

前端 未结 6 963
庸人自扰
庸人自扰 2020-11-27 22:00

I need the following, Can anyone please help me do it.

Rank   Cust_Type   Cust_Name   Revenue
1      Top         A           10000
2      Top         B               


        
6条回答
  •  失恋的感觉
    2020-11-27 22:21

    For JOIN tables problem, I found a solution.

    I create a temporary table, this way MySQL maintain the order of value that I want to rank.

    DROP TEMPORARY TABLE IF EXISTS tmp_mytable;
    
    CREATE TEMPORARY TABLE tmp_mytable ENGINE = MEMORY
        SELECT mytable.id AS id,
               mytable.login AS Login,
               cliente.myrank_id AS id_myrank,
               mytable.rankvalue AS rankvalue
        FROM mytable
       INNER JOIN myjoin ON (mytable.id_myjoin = myjoin.id)
        ORDER BY 3, 4 DESC;
    
    SELECT id, login, IFNULL(id_myrank, 0) AS id_myrank, rankvalue,
           @rank := IF(@prev_born = IFNULL(id_myrank, 0), @rank + 1, 1) AS ranking,
           @prev_Born := IFNULL(id_myrank, 0) AS fake_field
    FROM tmp_mytable, (select @prev_born := 0, @rank := 0) r
    -- HAVING ranking < 20;
    

    *PS: I tried with View creation, but insn't work too.

提交回复
热议问题