Excel:获取每个组的最常值

时间:2016-01-17 20:08:16

标签: excel ms-access data-processing

我有一个包含两列的表格(excel)(Time' hh:mm:ss',Value),我想为每组行获得最频繁的值。

例如我有

Time    | Value
4:35:49 | 122
4:35:49 | 122
4:35:50 | 121
4:35:50 | 121
4:35:50 | 111
4:35:51 | 122
4:35:51 | 111
4:35:51 | 111
4:35:51 | 132
4:35:51 | 132

我希望每次获得最频繁的价值

Time    | Value
4:35:49 | 122
4:35:50 | 121
4:35:51 | 132

提前致谢

更新 @scott with helper column的第一个答案是正确的

See the pic

3 个答案:

答案 0 :(得分:1)

您可以使用辅助列:

首先它需要一个辅助列,所以在C中我放

=COUNTIFS($A$2:$A$11,A2,$B$2:$B$11,B2)

然后在F2中我输入以下数组公式:

=INDEX($B$2:$B$11,MATCH(MAX(IF($A$2:$A$11=E2,IF($C$2:$C$11 = MAX(IF($A$2:$A$11=E2,$C$2:$C$11)),$B$2:$B$11))),$B$2:$B$11,0))

这是一个数组公式,必须使用Ctrl-Shift-Enter确认。然后复制下来。

我这样设置:

enter image description here

答案 1 :(得分:1)

以下是在MS Access中执行此操作的一种方法:

select tv.*
from (select time, value, count(*) as cnt
      from t 
      group by time, value
     ) as tv
where exists (select 1
              from (select top 1 time, value, count(*) as cnt
                    from t as t2
                    where t.time = t2.time
                    group by time, value
                    order by count(*) desc, value desc
                  ) as x
              where x.time = tv.time and x.value = tv.value
             );

MS Access不支持窗口函数或CTE等功能,可以在其他数据库中更轻松地进行此类查询。

答案 2 :(得分:0)

那会有用吗?我没有尝试并获得灵感here

;WITH t3 AS
(
   SELECT *,
         ROW_NUMBER() OVER (PARTITION BY time ORDER BY c DESC, value DESC) AS rn
   FROM (SELECT COUNT(*) AS c, time, value FROM t GROUP BY time, value) AS t2
)
SELECT *
FROM t3
WHERE rn = 1