Add up and count

INPUT · Slides

Gathering by two things

01 / 04

Per stall, and per day as well

You can get a total per stall. You can get a total per day. So how do you get "what did Komorebi Studio take on day one?"

Name both in a WHERE and you get that one square. But you want all the squares. You stack two things to gather by.

02 / 04

Just separate them with a comma

Put columns after GROUP BY separated by commas and rows with the same combination become one bundle.

Three stalls by two days, so up to six groups. Line both columns up in SELECT too and you can tell which square you are looking at.

SELECT stall, day, SUM(sold)  FROM sales  GROUP BY stall, day;

Result

Komorebi Studio | 2026-05-02 | 12
Komorebi Studio | 2026-05-03 | 6
Tsumugi Goods | 2026-05-02 | 4
Tsumugi Goods | 2026-05-03 | 20
Yurari Workshop | 2026-05-02 | 2
Yurari Workshop | 2026-05-03 | 4

03 / 04

The groups get finer

The more things you gather by, the fewer rows land in each group and the more rows come back. Look at COUNT(*) and some groups hold only one row.

Go too fine and the aggregating stops meaning much. Let what you want to compare decide how many you gather by.

SELECT stall, day, COUNT(*)  FROM sales  GROUP BY stall, day;

Result

Komorebi Studio | 2026-05-02 | 2
Komorebi Studio | 2026-05-03 | 1
Tsumugi Goods | 2026-05-02 | 1
Tsumugi Goods | 2026-05-03 | 2
Yurari Workshop | 2026-05-02 | 1
Yurari Workshop | 2026-05-03 | 1

04 / 04

The order you write shows in the order you get

Swap it round to GROUP BY day, stall and the groups hold the same rows, but the order changes. It gathers by day first, then the stalls line up inside.

Pick the order to suit whoever is reading. Day first for a daily report, stall first for a report on the stalls. Let us write some.

SELECT day, stall, SUM(price * sold)  FROM sales  GROUP BY day, stall;

Result

2026-05-02 | Komorebi Studio | 6600
2026-05-02 | Tsumugi Goods | 7200
2026-05-02 | Yurari Workshop | 4800
2026-05-03 | Komorebi Studio | 4200
2026-05-03 | Tsumugi Goods | 13800
2026-05-03 | Yurari Workshop | 4800