如何在ARRAYFORMULA中转换QUERY公式以自动填充每一行的QUERY

时间:2019-04-12 08:18:50

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

使用查询公式,我想计算A列(例如Apple)中某个字符串的所有重复出现,并同时检查在B列出现重复字符串的同一行中是否不为空。取决于计数是否大于0,我想在每一行的C列中显示一条消息。

在查询的WHERE子句中,我引用要在其中显示消息的A列的行号,如下所示:

…where A = '"&A1&"'…
…where A = '"&A2&"'…
…where A = '"&A3&"'…

...

=IF(IFERROR(QUERY($A:$B;"select COUNT(A) where A = '"&A1&"' and B is not
null LABEL COUNT(A) ''";0)) > 0;"B is not empty for duplicate strings";"B 
is empty for duplicate strings").  

结果应该是这样的:

+---+---------+---+--------------------------------------+--+
|   |    A    | B |                  C                   |  |
+---+---------+---+--------------------------------------+--+
| 1 | Apple   | 1 | B is not empty for duplicate strings |  |
| 2 | Apple   |   | B is not empty for duplicate strings |  |
| 3 | Orange  |   | B is empty for duplicate strings     |  |
| 4 | Orange  |   | B is empty for duplicate strings     |  |
+---+---------+---+--------------------------------------+--+

我想将此公式转换为数组公式并将其放入标题中,以便该公式自动展开。

到目前为止,我已经尝试过:

={"YOUR HEADER";ARRAYFORMULA(IF(IFERROR(QUERY($A:$B;"select COUNT(A) 
where A = '"&A2&"' and B is not null LABEL COUNT(A) ''";0)) > 0;"B is 
not empty for duplicate strings";"B is empty for duplicate strings"))}

这仅适用于第二行,因为我的查询引用了A2列。如何更改将检查每一行的公式?

1 个答案:

答案 0 :(得分:1)

在另一个论坛上,我得到了答案。对于任何有兴趣的人:

=ArrayFormula(IF(A2:A="";"";IF(QUERY(ArrayFormula(COUNTIF(QUERY(A2:B;"select A,
count(A) where B is not null group by A label count(A)''");A2:B));"select 
Col1")>0;"B is not empty for duplicate strings";"B is empty for duplicate strings")))