How to Group Data in Google Sheets, Using QUERY

The QUERY function, groups data based on unique combinations of Employee ID, Name, Rank, and Gender, eliminating duplicate entries and consolidating results for the same employees.

Here, the Google Sheets data contains information about employees, both male and female. Each employee is assigned a unique ID, and they receive reward payments on a quarterly basis.

Employee list

QUERY Function: Data List Group By Employee ID

The below formula, used in cell I2, applies the QUERY function in Google Sheets to summarize and organize employee reward data based on specific conditions. Here’s a breakdown of its functionality:

  • B2:G:Specifies the data range to query.
  • Select Col1, Col2, Col3, Col4, SUM(Col5) :
    • Selects columns 1 to 4 (Employee ID, Name, Rank, Gender).
    • Its calculates the total (SUM) of rewards from column 5 for each group.
  • Where Col6 is not null : Ensures only rows where Column 6 (Period) is filled are considered.
  • GROUP BY Col1, Col2, Col3, Col4 :
    • Groups the data based on unique combinations of Employee ID, Name, Rank, and Gender.
    • This avoids duplicate entries and aggregates rewards for the same employee.
  • 1 : Specifies that the headers are located in the first row of the range

QUERY Function:

=QUERY(B2:G,"Select Col1, Col2, Col3, Col4, SUM(Col5) Where Col6 is not null GROUP BY Col1, Col2, Col3, Col4",1)

Results:

Group by id using Query function

Output:

  • The query generates a summarized table displaying; Unique Employee ID, Name, Rank, Gender, and their Total Rewards across all periods.
  • It eliminates duplicate entries by grouping data and summing rewards for each employee.

If the data does not have unique values for Rank or Gender, the QUERY function will still group the data based on the specified columns (such as Employee ID, Name, Rank, and Gender) and aggregate the results for all rows with the same combination of values in these columns. However, the output may vary due to employees having multiple ranks.

=QUERY(B2:G,"Select Col1, Col2, Col3, Col4, SUM(Col5) Where Col6 is not null GROUP BY Col1, Col2, Col3, Col4",1)

Group by id using Query in Google Sheets

For instance, as shown in the image above, Gaganpreet Kaur is listed under both Junior Assistant and Clerk, while Abhinav Kumar appears with the titles of Peon and Teacher. These variations in rank will lead to separate rows in the output for each distinct combination of Name and Rank.

Here’s what happens specifically:

  • Non-unique Ranks or Genders:
    • The function treats each combination of Employee ID, Name, Rank, and Gender as a group.
    • If multiple rows share the same combination of these values, it sums up the rewards (or other aggregated fields) for those rows.
  • Impact on Results:
    • The data output will still be grouped, but duplicate rows with identical combinations will be merged into one, and the aggregated reward (or other specified calculation) will represent the total for those duplicates.

The QUERY function in Google Sheets is a powerful tool for efficiently organizing and analyzing reward data. It simplifies data modification and summarization, providing advanced capabilities that are more streamlined than conventional Excel techniques.

Is there a way to group rows based on cell value in a particular column into named range in Google Sheets?

  • Yes, use the QUERY function : First, apply the QUERY formula to group and filter your data.
  • Manually Create Named Ranges:
    • Select the output range of your query.
    • Go to Data > Named ranges.
    • Assign a name to that range (for example, “Clerk_Group”).

Need Help to Write Us:

If you have any questions, need assistance, or encounter challenges, feel free to share your concerns in the comments. The CountLen team is dedicated to providing quick and effective solutions. If you notice any inaccuracies or misleading information, please provide feedback—we’re here to support you!

CountLen Team
CountLen Team

CountLen Team is known for making complex topics accessible. Aiming to bridge the Excel and Google Sheets knowledge gap.

guest
0 Comments
Inline Feedbacks
View all comments