我有一個註冊,其所有交易與他們的帳戶ID。該寄存器有一個「類型」字段,我想在其中提取摘要數據。case/if/filter /?在谷歌工作表查詢
交易:
| 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
requires input of of a column identifier它似乎並不能使用任何非標量函數(如FILTER
)或甚至任何未列出的聚合函數(如JOIN
)。
我目前正在使用一堆單獨的查詢與不同的WHERE參數,但它非常緩慢。