QUERY以合并两个区域的名称并将它们的出现次数相加

问题描述 投票:1回答:3

我在英国有一个州/地区的阵列。一些区域在此列表中出现不止一次,因此我执行了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                     |
+----------------+-----------------------+

抱歉不共享表格,但我有安全限制,不允许我在公司外共享任何链接。

google-sheets google-sheets-formula google-sheets-query
3个回答
1
投票
=QUERY(QUERY(ARRAYFORMULA(
 {SUBSTITUTE(IF(A1:A="London","♥",A1:A),"♥","Greater London"),B1:B}),
 "select Col1, sum(Col2) 
  where Col1 is not null 
  group by Col1"),
 "select Col1, max(Col2) 
  group by Col1 
  order by max(Col2) desc 
  limit 5 
  label max(Col2)'Number of occurrences'",1)

0


1
投票
=QUERY(ARRAYFORMULA(SUBSTITUTE(
 IF((A1:A="London")+(A1:A="London2")+(A1:A="London3"),
 "♥",A1:A),"♥","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)

0


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)

0

© www.soinside.com 2019 - 2024. All rights reserved.