我有一个登记册,其中包含所有带有其帐户 ID 的交易。该寄存器有一个“类型”字段,我想在其上提取摘要数据。
交易:
| A | B | C | D |
|---------|---------|-------------|------------|
| Account | Amount | Type | Date |
|---------|---------|-------------|------------|
| 1b6f | 44.21 | Charge | 2016-09-01 |
| 5g0p | 101.57 | Charge | 2016-09-01 |
| 5g0p | 21.53 | Upgrade Fee | 2016-09-01 |
| 5g0p | -123.10 | Payment | 2016-09-07 |
| 1b6f | 4.43 | Late Fee | 2016-10-01 |
| 1b6f | 4.87 | Late Fee | 2016-11-01 |
我想要一个允许我提取以下所有摘要信息的查询
Account
Current Balance
Total Charges
Last Charge Date
First Charge Date
Total Fees
Last Fee Date
Total Payments
Last Payment Date
像这样的东西:
=QUERY(
A:D,
"SELECT A,
SUM(B),
SUM(FILTER(D:D,C:C='Charge')),
MAX(FILTER(D:D,C:C='Charge')),
MIN(FILTER(D:D,C:C='Charge')),
SUM(FILTER(B:B,ISNUMBER(FIND('Fee',C)))),
MAX(FILTER(D:D,ISNUMBER(FIND('Fee',C)))),
SUM(FILTER(B:B,C:C='Payment')),
MAX(FILTER(D:D,C:C='Payment'))
GROUP BY A
label
A 'Account',
SUM(B) 'Current Balance',
SUM(FILTER(D:D,C:C='Charge')) 'Total Charges',
MAX(FILTER(D:D,C:C='Charge')) 'Last Charge Date',
MIN(FILTER(D:D,C:C='Charge')) 'First Charge Date',
SUM(FILTER(B:B,ISNUMBER(FIND('Fee',C)))) 'Total Fees',
MAX(FILTER(D:D,ISNUMBER(FIND('Fee',C)))) 'Last Fee Date',
SUM(FILTER(B:B,C:C='Payment')) 'Total Payments',
MAX(FILTER(D:D,C:C='Payment')) 'Last Payment Date'
"
)
有了这些结果
| Account | Current Balance | Total Charges | Last Charge Date | First Charge Date | Total Fees | Last Fee Date | Total Payments | Last Payment Date |
|---------|-----------------|---------------|------------------|-------------------|------------|---------------|----------------|-------------------|
| 1b6f | 53.51 | 44.21 | 2016-09-01 | 2016-09-01 | 9.30 | 2016-11-01 | 0 | |
| 5g0p | 121.55 | 223.12 | 2016-10-01 | 2016-09-01 | 21.53 | 2016-09-01 | -123.10 | 2016-09-07 |
不幸的是,MAX
需要输入列标识符,而且您似乎根本不能使用任何非标量函数(例如FILTER
),甚至不能使用任何未列出的聚合函数(例如JOIN
)。
我目前正在使用一堆具有不同 WHERE 参数的单独查询,但是它非常非常慢。