MySQL 5.6-类似于DENSE_RANK的功能,无需订购

时间:2018-09-16 09:46:37

标签: mysql sql mysql-5.6

我有一张这样的桌子:

+------+-----------+
|caseID|groupVarian|
+------+-----------+
|1     |A,B,C,D,E  |
+------+-----------+
|2     |A,B,N,O,P  |
+------+-----------+
|3     |A,B,N,O,P  |
+------+-----------+
|4     |A,B,C,D,F  |
+------+-----------+
|5     |A,B,C,D,E  |
+------+-----------+

我想获得一个新列nameVarian,以使相同的groupVarian值具有由nameVarian表示的相同排名(例如:v1,v2等)。但是,分配给特定nameVarian的{​​{1}}值应按照groupVarian的顺序(在表中出现的顺序)。

输出应类似于:

caseID

3 个答案:

答案 0 :(得分:2)

您可以使用DENSE_RANK(MySQL 8.0):

SELECT *, CONCAT('v', DENSE_RANK() OVER(ORDER BY groupVarian)) AS namevarian
FROM tab
ORDER BY CaseID;

db<>fiddle demo

答案 1 :(得分:2)

对于 MySQL版本<8.0 OP's version is 5.6):

问题陈述似乎需要groupVarian上的DENSE_RANK功能;但是事实并非如此。 As explained by @Gordon Linoff

  

您似乎希望按照它们在广告中的显示顺序进行枚举   数据。

假设您的表名是t(请为您的代码相应地更改表名和字段名)。这是approach utilizing session variables(对于旧版本的MySQL ,为),给出了所需的结果( DB Fiddle ):

SET @row_number = 0;
SELECT t3.caseID, 
       t3.groupVarian, 
       CONCAT('v', t2.num) AS nameVarian
FROM
  (
   SELECT 
     (@row_number:=@row_number + 1) AS num, 
     t1.groupVarian 
   FROM 
     (
      SELECT DISTINCT groupVarian 
      FROM t 
      ORDER BY caseID ASC 
     ) AS t1 
  ) AS t2 
INNER JOIN t AS t3 
  ON t3.groupVarian = t2.groupVarian 
ORDER BY t3.caseID ASC 

此外:我之前的模拟DENSE_RANK功能的尝试效果很好。尽管也可以稍微调整以前的查询以实现DENSE_RANK功能。但是,以下查询会更高效,因为它会创建较小的派生表,并避免在groupVarian上使用 JOIN

SET @row_number = 1;
SET @group_varian = '';

SELECT inner_nest.caseID, 
       inner_nest.groupVarian, 
       CONCAT('v', inner_nest.num) as nameVarian 
FROM (
        SELECT 
            caseID, 
            @row_number:=CASE
                           WHEN @group_varian = groupVarian THEN @row_number
                           ELSE @row_number + 1
                         END AS num, 
            @group_varian:=groupVarian as groupVarian 
        FROM
            t  
        ORDER BY groupVarian
     ) AS inner_nest 
ORDER BY inner_nest.caseID ASC 

答案 2 :(得分:1)

基本上,您想枚举变体。如果您只想要一个数字,则可以使用最小ID:

select t.*, min_codeId as groupVariantId
from t join
     (select groupVariant, min(codeId) as min_codeId
      from t
      group by groupVariant
     ) g
     on t.groupVariant = g.groupVariant;

但这并不是您想要的。您似乎希望按它们在数据中出现的顺序来枚举它们。为此,您需要变量。这有点棘手,但是:

select t.*, rn as groupVariantId
from t join
     (select g.*,
             (@rn := if(@gv = groupvariant, @gv,
                        if(@gv := groupvariant, @gv+1, @gv+1)
                       )
             ) as rn
      from (select groupVariant, min(codeId) as min_codeId
            from t
            group by groupVariant
            order by min(codeId)
           ) g cross join
           (select @gv := '', @rn := 0) params
     ) g
     on t.groupVariant = g.groupVariant;

使用变量非常棘手。一个重要的考虑因素:MySQL不保证SELECT中表达式的求值顺序。这意味着变量不应该在一个表达式中赋值,然后在另一个表达式中使用,因为它们可能以错误的顺序求值(另一个答案有这个错误)。

此外,order by必须在子查询中进行。 MySQL不保证变量分配在排序之前发生。