QUERY 合并两个区域的名称并对它们出现的次数求和
QUERY to merge the names of two regions and sum the number of their occurrences
我在英国有一个 states/regions 数组。有些区域在此列表中出现不止一次,因此我执行了 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 |
+----------------+-----------------------+
我需要的是将 Greater London 和 London 条目合并到 Greater London 的名称下,并将它们的出现次数相加,提供以下结果:
+----------------+-----------------------+
| State/Province | Number of occurrences |
+----------------+-----------------------+
| Greater London | 9 |
+----------------+-----------------------+
| Cheshire | 6 |
+----------------+-----------------------+
| West Lothian | 3 |
+----------------+-----------------------+
| Kent | 2 |
+----------------+-----------------------+
| Hampshire | 1 |
+----------------+-----------------------+
抱歉没有分享 sheet,但我有安全限制不允许我分享任何 link 到 sheet公司外.
=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)
=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)
=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)
我在英国有一个 states/regions 数组。有些区域在此列表中出现不止一次,因此我执行了 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 |
+----------------+-----------------------+
我需要的是将 Greater London 和 London 条目合并到 Greater London 的名称下,并将它们的出现次数相加,提供以下结果:
+----------------+-----------------------+
| State/Province | Number of occurrences |
+----------------+-----------------------+
| Greater London | 9 |
+----------------+-----------------------+
| Cheshire | 6 |
+----------------+-----------------------+
| West Lothian | 3 |
+----------------+-----------------------+
| Kent | 2 |
+----------------+-----------------------+
| Hampshire | 1 |
+----------------+-----------------------+
抱歉没有分享 sheet,但我有安全限制不允许我分享任何 link 到 sheet公司外.
=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)
=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)
=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)