获取mysql中多个等级的平均值

时间:2014-10-27 10:40:42

标签: mysql correlation

我正在尝试计算mysql中某些数据的spearmans等级相关性。为此,我需要按降序排列数据。我得到了这个工作但是当2行具有相同的变量时,等级应该是2个或更多等级的平均值。 作为一个例子,这里是一些具有当前排名和预期排名的示例数据

| id|var|rank|
| 8 | 1 | 1  |
| 2 | 2 | 2  | # rank should be 2.5
| 6 | 2 | 3  | # rank should be 2.5 
| 4 | 3 | 4  | 
| 5 | 4 | 5  | 
| 1 | 5 | 6  | 
| 3 | 6 | 7  | # rank should be 8
| 7 | 6 | 8  | # rank should be 8
| 9 | 6 | 9  | # rank should be 8

我的查询现在看起来像这样:

SET @rownum := 0;
SET @rownum2 := 0;
SELECT  rank_x.id, rank_x.var1, rank_x.rk_x
FROM 
    (SELECT id, @rownum := @rownum + 1 AS rk_x, var1
     FROM sampledata order by var1 asc) as rank_x;

1 个答案:

答案 0 :(得分:0)

您可以通过分配序号然后取平均值来完成此操作。这需要一些嵌套的子查询,但是可行。这个想法是:

  • 首先分配顺序值
  • 然后找到每个id的最大值。
  • 然后找到分钟
  • 然后取平均值

查询如下:

SELECT id, var1, (minrn + maxrn) / 2
FROM (SELECT sd.*,
             (@maxrn := if(@v2 = var1, @maxrn,
                           if(@v2 := var1, rn, rn)
                          )
             ) as maxrn
      FROM (SELECT  sd.*,
                    (@minrn := if(@v = var1, @minrn,
                                  if(@v := var1, rn, rn)
                                 )
                    ) as minrn
            FROM (SELECT id, var1, (@rn := @rn + 1) as rn
                  FROM sampledata sd CROSS JOIN
                       (SELECT @rn := 0) vars
                  ORDER BY var1 asc
                 ) sd CROSS JOIN
                 (SELECT @minrn := 0, @v := -1) vars
            ORDER BY var1, rn
           ) sd CROSS JOIN
           (SELECT @maxrn := 0, @v2 := -1) vars
      ORDER BY var1, rn desc
     ) sd;