Excel :获取每组出现次数最多的值
Excel : Get the most frequent value for each group
我有一个 table ( excel ) 有两列 ( 时间 'hh:mm:ss' , 值 ) 我想为每组行获取最频繁的值。
例如我有
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 列的第一个答案是正确的
这样行吗?我还没有尝试并受到启发 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
您可以使用辅助列:
首先它需要一个辅助列,所以在 C 中我输入了
=COUNTIFS($A:$A,A2,$B:$B,B2)
然后在 F2 中输入以下数组公式:
=INDEX($B:$B,MATCH(MAX(IF($A:$A=E2,IF($C:$C = MAX(IF($A:$A=E2,$C:$C)),$B:$B))),$B:$B,0))
为数组公式,必须用Ctrl-Shift-Enter确认。然后复制下来。
我是这样设置的:
这是在 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 不支持 window 函数或 CTE 等功能,这些功能使此类查询在其他数据库中更容易。
我有一个 table ( excel ) 有两列 ( 时间 'hh:mm:ss' , 值 ) 我想为每组行获取最频繁的值。
例如我有
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 列的第一个答案是正确的
这样行吗?我还没有尝试并受到启发 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
您可以使用辅助列:
首先它需要一个辅助列,所以在 C 中我输入了
=COUNTIFS($A:$A,A2,$B:$B,B2)
然后在 F2 中输入以下数组公式:
=INDEX($B:$B,MATCH(MAX(IF($A:$A=E2,IF($C:$C = MAX(IF($A:$A=E2,$C:$C)),$B:$B))),$B:$B,0))
为数组公式,必须用Ctrl-Shift-Enter确认。然后复制下来。
我是这样设置的:
这是在 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 不支持 window 函数或 CTE 等功能,这些功能使此类查询在其他数据库中更容易。