由於對 Google sheet 內建函式不常用,但腦海總浮現之前查過的印象,還是記錄一下好了。
Group 測試數據:
GroupColumn | CountColumn | SumColumn |
---|---|---|
AAA | 10 | 100 |
BBB | 20 | 200 |
CCC | 30 | 300 |
AAA | 40 | 400 |
以GroupColumn
進行 GroupBy 預期應該是:
GroupColumn | CountColumn | SumColumn |
---|---|---|
AAA | 50 | 500 |
BBB | 20 | 200 |
CCC | 30 | 300 |
語法:
1 | =QUERY(rawData!A1:C4,"select A, count(B), sum(C) group by A") |
rawData
為 sheet 名稱A1:C4
為資料範圍
如果想要替 Column 命名,需使用 label
語法,例如 label A 'AliasName'
完整如下:
1 | =QUERY(rawData!A1:C4,"select A, sum(B), sum(C) group by A label A 'GroupColumn', sum(B) 'CountColumn', sum(C) 'SumColumn'") |