我在英国有一个州/地区的阵列。一些区域在此列表中出现不止一次,因此我执行了COUNTIF
来确定每个区域出现的次数。
现在我需要运行QUERY
列出前5个区域。
通常,大多数事件发生在伦敦地区。
问题在于,在这些地区有2个州指的是大伦敦地区 - 伦敦和大伦敦。
这两个我需要合并并求和它们的值。只需要一个地区 - 大伦敦,它的价值需要持有伦敦和大伦敦的总和。
这是我的数据集:
+----------------+-------+
| State/Province | count |
+----------------+-------+
| Hampshire | 1 |
+----------------+-------+
| Kent | 2 |
+----------------+-------+
| West Lothian | 3 |
+----------------+-------+
| London | 4 |
+----------------+-------+
| Greater London | 5 |
+----------------+-------+
| Cheshire | 6 |
+----------------+-------+
到目前为止,我已设法将这个QUERY
放在一起:
=QUERY(A1:B,"select A, max(B) group by A order by max(B) desc limit 5 label max(B) 'Number of occurrences'",1)
这给了我这个输出:
+----------------+-----------------------+
| State/Province | Number of occurrences |
+----------------+-----------------------+
| Cheshire | 6 |
+----------------+-----------------------+
| Greater London | 5 |
+----------------+-----------------------+
| London | 4 |
+----------------+-----------------------+
| West Lothian | 3 |
+----------------+-----------------------+
| Kent | 2 |
+----------------+-----------------------+
我需要的是大伦敦和伦敦的条目将以大伦敦的名义合并,并将它们的出现次数相加,得出以下结果:
+----------------+-----------------------+
| State/Province | Number of occurrences |
+----------------+-----------------------+
| Greater London | 9 |
+----------------+-----------------------+
| Cheshire | 6 |
+----------------+-----------------------+
| West Lothian | 3 |
+----------------+-----------------------+
| Kent | 2 |
+----------------+-----------------------+
| Hampshire | 1 |
+----------------+-----------------------+
抱歉不共享表格,但我有安全限制,不允许我在公司外共享任何链接。
=QUERY(ARRAYFORMULA(SUBSTITUTE(
IF((QUERY(A1:B,"where B=1")="London")+
(QUERY(A1:B,"where B=1")="London2")+
(QUERY(A1:B,"where B=1")="London3"),
"♥",QUERY(A1:B,"where B=1")),"♥","Greater London")),
"select Col1, count(Col1)
where Col1 is not NULL and not Col1 = '#N/A'
group by Col1
order by count(Col1) desc
limit 5
label count(Col1) 'Number of occurrences'", 1)