Google SpreadSheet 查询 - 将查询结果合并为一个

Google SpreadSheet Query - Merge queries results into one

让我们把这些数据放在 Google sheet:

| Product | Green | Red | Date      |
| A       | 1     | 0   | 1/1/2020  |
| A       | 1     | 0   | 2/1/2020  |
| B       | 0     | 1   | 2/25/2020 |
| C       | 1     | 0   | 2/28/2020 |
| A       | 0     | 1   | 3/1/2020  |

我的目标是显示每个产品的绿色/红色总和:

我创建了这个 Google 查询来获取全年的结果:

=QUERY(DATA!A:D,"select A, sum(B), sum(C) where D >= date '2020-01-01' and D <= date '2020-12-31' group by A")

我得到这个结果:

| Product | sum Green | sum Red |
| A       | 2         | 1       |
| B       | 0         | 1       |
| C       | 1         | 0       |

以及给定月份的查询(我简化了查询,但我有一个设置 sheet 来指定要查询的月份):

=QUERY(DATA!A:D,"select A, sum(B), sum(C) where D >= date '2020-01-01' and D <= date '2020-01-31' group by A")

得到这个结果:

| Product | sum Green | sum Red |
| A       | 1         | 0       |

现在我坚持将两个结果合二为一,如下所示:

| Product | Year sum Green | Year sum Red | Jan sum Green | Jan sum Red |
| A       | 2              | 1            | 1             | 0           |
| B       | 0              | 1            |               |             |       
| C       | 1              | 0            |               |             |

我怎样才能做到这一点?

非常感谢您的帮助!

尝试:

=ARRAYFORMULA(IFNA(VLOOKUP(F2:F, QUERY(DATA!A:D, 
 "select A,sum(B),sum(C) 
  where month(D)+1 = 1 
  group by A 
  label sum(B)'Jan sum Green',sum(C)'Jan sum Red'"), {2,3}, 0)))