Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How do I summarize rows by category with GROUP BY?

Quick answer

Use GROUP BY on the category column and an aggregate such as SUM on the numeric column: SELECT category, SUM(quantity) AS total_quantity FROM inventory GROUP BY category;. Each distinct category produces one result row, with the sum calculated from the rows in that group. If you select another descriptive column, include it in the grouping or aggregate it intentionally.

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I have inventory rows with a category and a quantity. How do I get one total per category?

Geez

Assuming your table is named inventory and its columns are category and quantity, use:

SELECT category, SUM(quantity) AS total_quantity
FROM inventory
GROUP BY category;

GROUP BY category gathers rows with the same category into groups, and SUM(quantity) adds the quantities within each group. Each group contributes one result row. [S1]

For example, suppose the inventory contains these rows:

category   quantity
Adapters   5
Cables     10
Cables     2
Widgets    4
Widgets    3

The query returns:

category   total_quantity
Adapters   5
Cables     12
Widgets    7

These are illustrative values, not data from a real inventory. The table and column names must match your own schema.

Follow-up question

Why do I get one row per category instead of a total for every original row?

Geez

GROUP BY changes the level of the result: rows sharing the same grouping value belong to one group, and each group contributes one result row. Then SUM is evaluated across the rows in that group. [S1]

In the example, both Cables rows are combined, so their quantities, 10 and 2, become one total of 12. If you want to inspect individual rows instead, select the rows without grouping them.

Follow-up question

Can I also show a category description in the result?

Geez

If your table has a category_description column, decide what one description should represent each group. If the description is determined by category and you want it shown as a grouped field, include it in the grouping:

SELECT category, category_description, SUM(quantity) AS total_quantity
FROM inventory
GROUP BY category, category_description;

This groups by pairs of category and description. Check that those pairs correspond to the groups you intend; a different description under the same category would form a separate group. Another option is to aggregate the description using a function appropriate to your data and database, rather than selecting it as an ungrouped value.

Avoid selecting a column such as item_name without grouping or aggregating it. In SQLite, such a bare column can come from an arbitrary row in the group, so it may not be a dependable description of the total. Other database engines may reject that query. [S1]

Follow-up question

What should I check if the totals look wrong?

Geez

First confirm the intended group: each distinct category value is treated as a separate group. Then check that the values being added are in quantity, and that the query names the intended table and columns. If you add a filter, check whether it excludes rows that should contribute to the totals.

A useful way to reason about the result is to pick one category, list its input rows, and add their quantities by hand. For the example, Cables has 10 and 2, so its expected total is 12. If you need a different grouping—such as totals per category and location—group by both fields and expect a separate total for each category-location pair. This guide illustrates SQLite's grouping behavior; the cited material does not establish how your particular database, schema, or data is configured.

A practical checklist

  • Replace inventory, category, and quantity with the names in your table.

  • Use SUM for totals; use a different aggregate only if it matches the question you are answering.

  • Group by every descriptive field you intend to show as part of each result row.

  • Check whether filters exclude rows that should be included in the totals.

  • Verify one example category by adding its contributing rows.

A few more useful answers

What if I want totals by category and location?

Select both category and location, then group by both. Each distinct category-location combination will have its own result row and total.

Why does SQLite allow a selected column that is not grouped or aggregated?

SQLite permits this as an extension, but the bare column's value may come from an arbitrary row in its group. Include the column in GROUP BY or aggregate it intentionally; other database engines may apply stricter rules. [S1]

Sources

References used for this guide.

  1. [S1] SQLite SELECT — GROUP BY, aggregates, and bare columns
    sqlite.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work