Combine Similar Rows and Sum Values in Google Sheets

Published on

QUERY and Pivot Table are two options in Google Sheets for combining and summing duplicate or similar rows. However, you can also use a unique combination formula with UNIQUE and SUMIF. In this tutorial, I’ll detail all three options so you can choose the one that best suits your needs.

For clarity, I’m using a very basic data set so that you can easily follow the steps.

In the first example, I’ll use a two-column data set: names in the first column (B1:B) and amounts in the second column (C1:C).

NameAmount
Alice100
Prashanth200
Alice200
Ben300
Prashanth50
Alice150
Ben100
Prashanth160
Anya175
Anya200

In the final part of this tutorial, you’ll learn how to apply these techniques to multiple columns for similar calculations.

Combine Similar Rows and Sum Values: Single-Column Grouping

QUERY is one of the quintessential functions for data manipulation in Google Sheets. So let’s start with that.

Combine Similar Rows and Sum Values (Category-Based)

Option 1: QUERY

Even if you’re unfamiliar with the QUERY function, you can follow my formula to gain the basic skills needed to combine similar rows and sum values in Google Sheets.

Formula:

=QUERY(B1:C11, "SELECT B, SUM(C) GROUP BY B", 1)

This formula selects the names in column B and calculates the total in column C by grouping the names in column B.

If you want to include an unlimited number of rows in the QUERY to account for future values, modify the formula as follows:

=QUERY(B1:C, "SELECT B, SUM(C) WHERE B IS NOT NULL GROUP BY B", 1)

Option 2: SUMIF and UNIQUE

This method involves two steps:

  1. First, enter the following UNIQUE formula in cell E2 to get the unique categories (names) in column B:
=UNIQUE(B2:B)
  1. Then, in cell F2, enter the following SUMIF array formula to sum the values in the range C2:C that match the unique categories in B2:B:
=ArrayFormula(SUMIF(B2:B, E2:E, C2:C))

This formula may leave trailing zeros where E2:E is empty. If you want to avoid that, use this formula instead:

=ArrayFormula(IF(E2:E="",,SUMIF(B2:B, E2:E, C2:C)))

That’s how we combine similar rows and sum values using the SUMIF and UNIQUE combination. Now, let’s move on to the Pivot Table.

Option 3: Pivot Table

Pivot Table for Combining Duplicate Rows and Summing Values

If you’re not comfortable with formulas, a Pivot Table is your best option.

A Pivot Table is a built-in tool for summarizing and analyzing data in popular spreadsheet applications. You can use it to combine and sum similar rows in Google Sheets as follows:

  1. Select the range B1:C.
  2. Click Insert > Pivot Table.
  3. Click Create.
  4. This will open a new sheet with the Pivot Table layout and a sidebar panel on the right-hand side.
  5. In the sidebar, drag and drop the “Name” field (the group column header) under “Rows.”
  6. Then, drag and drop the “Amount” field (the sum column header) under “Values.”
  7. Drag and drop the “Name” field under “Filter.”
  8. In the Filter field, click the “Show all items” drop-down and uncheck “(Blanks).”

This is the non-formula approach to combining similar rows and summing values.

Combine Duplicates in Rows and Sum Values (Multiple Columns)

This time we have a three-column data set where column B contains names, column C contains areas, and column D contains amounts.

Combine Similar Rows and Sum Values (Category and Subcategory-Based)

For this data set, you can use either QUERY or a Pivot Table.

I don’t recommend the SUMIF and UNIQUE combination as it won’t automatically expand the results. To handle this, you might consider combining names and areas or using the MAP LAMBDA function, which can be complex for beginners.

Here is the QUERY formula to combine duplicates in rows and sum values based on the category and subcategory columns:

=QUERY(B1:D, "SELECT B, C, SUM(D) WHERE B IS NOT NULL GROUP BY B, C", 1)

If you prefer using a Pivot Table, the only change compared to the two-column data set is:

  • Drag and drop the Area field under “Rows,” below the Name field.

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.

Lookup Values Under Categories in Excel and Google Sheets

We can use a combination of XLOOKUP and VLOOKUP in both Excel and Google...

Extract Items Under a Category from the Same Column in Excel

In Excel, you can use a combination of the XLOOKUP and DROP functions to...

How to Create a Searchable Table in Excel Using the FILTER Function

Finding specific records, or rows containing the required information, is straightforward in Excel using...

Time Sequences in Excel by Minute, Hour, or Second Increments

Creating time sequences, whether by hour, minute, or second increments in Excel, can be...

More like this

XMATCH Row by Row: Finding Values Across a Range in Google Sheets

Using the BYROW function with XMATCH in Google Sheets allows us to match values...

Limit Formula Expansion to a Specific Row in Google Sheets

In this tutorial, I’ll explain how to limit the expansion of an array formula...

3-D Referencing Structured Data Tables in Google Sheets

When you have several tables within a single sheet—not across multiple sheets in a...

15 COMMENTS

  1. Hi, I’m working on similar to it.

    I need to combine a duplicate row and sum the values of 2 columns differently.

    After getting that, I need to combine my filter function with the query function.

    Filter Function:

    =filter(C5:E,A5:A=B1) [this is for the dropdown category]

  2. Hello,

    I am doing something similar, but I need to separate the data by date. I cannot seem to get the formula right. Can you assist?

    • Hi, Janira Iglesias,

      Here is an example.

      Data in Columns:

      A – Dates
      B – Item Description
      C – Quantity.

      The formula in D1:

      =query(A1:C,"Select A,B,sum(C) where A is not null group by A,B")

      Please try it first. If that doesn’t help, make a sample and share that Sheet’s address/URL via the comment below.

  3. Hi Prashanth,

    hope you are well. First of all thanks so much for sharing your knowledge with the world. Very highly appreciated.

    I would have one question sum values part. I used your Merge/Combine rows solutions and it’s working fine for my use case. Except for one part.

    I’m using it for consolidation Orders from an e-commerce shop so I get one row per Order showing all different order positions and items that are in one basket/order.

    So origin Data shows two rows when two items are ordered. In one column it says “order position number” ..and because it’s two items its two rows with “order position number” #1 and the next row #2.

    I would like to not sum up but only show in the final table “order positions: #2” (as there are two items). Summing up would show #3.

    So is there a way to either only take the second value = #2 or identify the highest value? (it could also be somebody orders 5 times the same item, then I would have 1 row for the order showing order positions =5)

  4. Hello, are you still taking inquiries? I am trying to consolidate two sets of information that have repeating values (as shown in your video) but I would like a different result. Are you still monitoring this?

    Thanks!

    • Hi, Louie N,

      Nope! But I’ll try to help you in my leisure if you can share with me a sample of what you are working on. Use the ‘Reply’ below to share yours sheet. The link won’t be published here to protect your privacy.

      NB: Just a sample in 5-10 rows and a few columns.

    • The below formula is as per the sample data and the formula used in the example in my tutorial.

      =ArrayFormula(if(len(G4:G),sumif(B3:B&C3:C,G4:G&H4:H,D3:D),))

      In your case, it might be len(K2:K)

      Best,

  5. Hey Prashanth,

    I’m still having issues doing this. Since I can’t post a screenshot, I will try to explain it here.

    Column C has Multiple Values (Will Continue to grow with different Values)
    Column D has Multiple Values (Will Continue to grow with different Values)
    Column E is what I need the SUM for and is numeric

    Thank you in advance,

    Adam B. Yuro

    • Hi, Adam,

      You can try this Query. In this row#1 is considered as the header row.

      =query(C1:E,"Select C,D, sum(E) where C is not null group by C,D",1)

      If there is no header, then the Query formula would be like this.

      =query(C1:E,"Select C,D, sum(E) where C is not null group by C,D",0)

      Both of these formulas won’t work if you are from EU countries. Then use the Query as below.

      =query(C1:E;"Select C,D, sum(E) where C is not null group by C,D";1)

      If not helpful? Then try to share your Sheets’ link (no personal/confidential data)

      Best,

  6. This is great and thank you! Is there a way to not make the resulting data a new set of columns, but rather rearrange and modify the original data to show the sums?

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.