How to Use Group By using Query function

Using the Query function in Google Sheets allows you to efficiently analyze and aggregate data from a range of cells. The Group By clause helps you group your data by one or more columns, so you can perform calculations on each group, such as sum, count, or average.

Here are the steps to use the Group By clause with the Query function:

  1. Prepare your data in a Google Sheet. Ensure that the first row contains headers for each column.
  2. Identify the range of cells that you want to analyze using the query function. For example, A1:D100.
  3. Determine the columns you want to group by and the aggregation function(s) you want to apply to other columns.
  4. In an empty cell, write the Query function using the following syntax:
=QUERY(range, "SELECT columns, aggregation_function(column) GROUP BY columns")

Replace range with the range of cells you want to analyze, columns with the column(s) you want to group by, and aggregation_function(column) with the aggregation function(s) and column(s) you want to apply it to.

Example

Let's say you have a data set in range A1:C10 with the following columns: Date (A), Salesperson (B), and Sales (C). You want to find the total sales for each salesperson.

  1. First, make sure your data is properly formatted with headers in the first row.
  2. Identify the range of cells to analyze: A1:C10.
  3. You want to group by the Salesperson column (B) and find the sum of the Sales column (C) for each group.
  4. In an empty cell, write the Query function with the Group By clause:
=QUERY(A1:C10, "SELECT B, SUM(C) GROUP BY B")

After pressing Enter, you will see a new table that shows the total sales for each salesperson.

Did you find this useful?