How can I show each group’s total beside every detail row?
Quick answer
Use SUM(amount) OVER (PARTITION BY group_key) in your SELECT. The window sum calculates a total separately for each group while retaining the individual rows, unlike a grouped result that collapses rows. For example, partitioning invoice lines by customer shows each line and the same customer total beside every line. [S1]
The question, the details, the next step.
A practical checklist
Identify the detail rows that must remain visible.
Choose the column that defines one group.
Select the amount column to sum.
Add
SUM(amount) OVER (PARTITION BY group_column)and give the result a clear alias.Check a small sample: detail rows should remain, and rows in each group should show the same total.
A few more useful answers
Does the window sum remove duplicate detail rows?
No. It calculates a total over the partition while retaining result rows. If the input has duplicate rows, the query does not deduplicate them. [S1]
Will `GROUP BY` give me the same detail-level output?
Not by itself: grouping produces grouped results rather than preserving each input detail row. Use the window sum when you need both the detail and its group total. [S1]
Sources
References used for this guide.
- [S1] SQLite Window Functions: aggregate functions over a window sqlite.org · Checked
