How to Aggregate Strings in Google Sheets Using QUERY

Published on

By now, you may have tried different formula variations to aggregate text strings using the QUERY function in Google Sheets. No success, right?

You’re not alone. Many users wonder how to use aggregate functions in QUERY for strings—it’s a topic often discussed online.

It’s commonly believed that Google Sheets QUERY doesn’t support text string aggregation, and that’s only partially true.

Why Only Partially?

Because you can use the aggregation functions MIN or MAX in the QUERY SELECT clause with strings! These functions can help achieve our goal.

Note: It’s not possible to use CONCATENATE or other text join functions directly within QUERY.

Aggregate Strings in Google Sheets with QUERY: What Does It Mean?

Let’s clarify this concept with an example.

My sample data is in columns A and B, where A contains country names, and B contains Miss World Pageant winners. The dataset includes three countries for simplicity and clarity.

Example of aggregating text strings using the QUERY function in Google Sheets

Using QUERY, the data in column A (countries) can be grouped, and the corresponding values in column B (winners) can be aggregated.

To achieve this, we’ll use a helper column. If you’re not keen on adding a helper column, we’ll also explore an alternative with a virtual helper column.

Example: Aggregate Strings Using QUERY in Google Sheets

Helper Column Approach:

We’ll use the sample dataset mentioned earlier.

Step 1:

Since this is a helper column approach, enter the following formula in cell C2 (the helper column):

=ARRAYFORMULA(IF(LEN(A2:A), COUNTIFS(A2:A, A2:A, ROW(A2:A), "<=" & ROW(A2:A)),))

This formula generates a sequential count for each group of data in column A. For a detailed explanation of this formula, refer to my tutorial: Running Count in Google Sheets: Formula Examples.

Step 2:

Use the following QUERY formula in cell E2 to aggregate the text strings:

Formula 1:

=QUERY(QUERY(A2:C, "Select Col1, max(Col2) where Col1 is not null group by Col1 Pivot Col3"), "Select * offset 1", 0)

This formula aggregates the strings, and the result will match the image above.

Without Using a Helper Column:

If you prefer not to use a helper column, you can use the formula below directly in cell E2, skipping Step 1:

Formula 2:

=QUERY(QUERY({A2:B, ARRAYFORMULA(IF(LEN(A2:A), COUNTIFS(A2:A, A2:A, ROW(A2:A), "<=" & ROW(A2:A)),))}, "Select Col1, max(Col2) where Col1 is not null group by Col1 Pivot Col3"), "Select * offset 1", 0)

Here, the helper column formula from Step 1 is integrated into the QUERY as a virtual column.

Both formulas work seamlessly with sorted or unsorted group column data. However, sorted data has one advantage: the first column of the aggregated data will naturally be in sorted order. The aggregated strings, however, will not be sorted.

If you want the aggregated strings to also be in sorted order, sort the source data first. To do this, select the range A1:B, then click Data > Sort range > Advanced range sorting options. Check the box for “Data has a header row,” select “Sort by Country” in ascending order, and click “Add another sort column.” Choose “Winners” and sort it in ascending order as well.

Formula Explanation

Inner QUERY:

The inner QUERY aggregates the strings.

  • QUERY(A2:C, "Select Col1, max(Col2) where Col1 is not null group by Col1 Pivot Col3")
    • Select Col1, max(Col2): Groups the data by Col1 (Country) and computes the maximum value of Col2 (Winners) for each group.
    • Pivot Col3: Distributes the results across columns based on the values in Col3 (Helper column). This ensures the “max” winner for each running count is arranged in its corresponding position.
Aggregated strings using QUERY with pivot, including header (helper column values)

The result of this QUERY is a table where:

  • Each row corresponds to a unique value in Col1 (Country).
  • Columns correspond to Col3 (Helper values), showing the “max” winners for each sequence.

Outer QUERY:

The outer QUERY cleans up the aggregated strings by removing the header (Helper Column Values).

  • QUERY(..., "Select * offset 1", 0)
    • Select *: Retains all columns from the inner QUERY result.
    • Offset 1: Skips the first row (header row generated by the pivot).

This is how the formula Aggregate Strings in Google Sheets Using QUERY.

Resources

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Guide Prashanth KV brings a wealth of experience in Google Sheets and Excel, cultivated through years of work with multinational corporations in Mumbai and Dubai. As a recognized Google Product Expert in Docs Editors, Prashanth shares his expertise through insightful blogging since 2012. Explore his blog for practical tips and guidance on maximizing your spreadsheet skills.

How to Calculate Maximum Drawdown in Excel and Google Sheets

You can use the following dynamic array formula to calculate maximum drawdown (MDD) in...

Running Count with Structured References in Google Sheets

Running a count with structured references is achievable in Google Sheets tables using the...

Running Total with Structured Table References in Google Sheets

You can use two types of formulas to create a running total with structured...

Automate Multi-Colored Line Charts in Google Sheets

Usually, a line chart with a single line supports only one color. To create...

More like this

Running Count with Structured References in Google Sheets

Running a count with structured references is achievable in Google Sheets tables using the...

Running Total with Structured Table References in Google Sheets

You can use two types of formulas to create a running total with structured...

Automate Multi-Colored Line Charts in Google Sheets

Usually, a line chart with a single line supports only one color. To create...

4 COMMENTS

  1. Hi,

    I have tried manipulating this several times. But the output only returns two columns: the country and the first pageant winner for the country.

    I’ve even created a similar database and copy-pasted the formula. Unfortunately, still no change.

    Has there been an update to the max() function?

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.