查询以合并两个区域的名称并求和它们的出现次数

时间:2019-02-24 14:37:59

标签: google-sheets google-sheets-formula google-sheets-query

我有一个包含英国各州/地区的阵列。在此列表中,某些区域不止一次出现,因此我执行了COUNTIF来确定每个区域出现的次数。

现在,我需要运行QUERY来列出前5个区域。

通常,大多数情况发生在伦敦地区。

问题在于,在该地区有两个涉及大伦敦地区的州-伦敦和大伦敦。

这两个我需要合并并求和。 只需存在一个地区-大伦敦,它的价值就可以容纳伦敦和大伦敦的总和。

这是我拥有的数据集:

+----------------+-------+
| 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                     |
+----------------+-----------------------+

道歉,不共享工作表,但我有安全限制,不允许我在公司外部共享到工作表的任何链接。

3 个答案:

答案 0 :(得分: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 :(得分: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

答案 2 :(得分: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