在 Google 电子表格中按日期分组的字符串出现次数

Count of occurrence of a string group by day in Google spreadsheet

我在 Google 电子表格中有 table 数据,如下所示:

Date|Diet
4-Jan-2020|Coffee
4-Jan-2020|Snacks
4-Jan-2020|xyz
4-Jan-2020|Coffee
5-Jan-2020|Snacks
5-Jan-2020|abc
6-Jan-2020|Coffee
6-Jan-2020|Snacks

这个table是我每天吃的食物清单。我想知道我每天喝咖啡的次数。所以我想得到这样的输出:

Date | No of times I had Coffee    
4-Jan-2020| 2
5-Jan-2020| 0
6-Jan-2020| 1

我使用这个查询来获取输出。

=query(A1:B1425,"select A, COUNT(B) where B='Coffee' group by A")

通过此查询,我得到以下输出。请注意,我没有喝咖啡的那些日子

4-Jan-2020| 2
6-Jan-2020| 1

所以缺少 5-Jan-2020 的计数,因为那天没有字符串“Coffee”。

如何获得包括计数 0 在内的所需输出?谢谢。

您可能希望将计数器更改为 If 语句。 类似“IF(COUNT(B) where B='Coffee' group by A”>0,COUNT(B) where B='Coffee' group by A”,0)。 这将强制计数器具有实际值 (0),即使未找到任何内容

尝试:

=ARRAYFORMULA({UNIQUE(FILTER(A1:A, A1:A<>"")), 
  IFNA(VLOOKUP(UNIQUE(FILTER(A1:A, A1:A<>"")), 
 QUERY(A1:B, 
 "select A,count(B) 
  where B='Coffee' 
  group by A 
  label count(B)''"), 2, 0))*1})


或尝试:

=ARRAYFORMULA(QUERY({A1:B, IF(B1:B="coffee", 1, 0)}, 
 "select Col1,sum(Col3)
  where Col1 is not null  
  group by Col1 
  label sum(Col3)''"))