I’m trying to get a running sum in sqlite. Right now I have:
SELECT
type,
count,
SUM(count) as startIndex
FROM
(
SELECT
things.type,
COUNT(things.name) as count
FROM
things
INNER JOIN thingGroups
ON thingID = things.id
INNER JOIN groups
ON groups.id=thingGroups.groupID
WHERE
groups.name='Space'
GROUP BY things.type
ORDER BY type
)
GROUP BY type, count
This gives me:
Name A, 2, 2
Name B, 3, 3
Name C, 3, 3
I’m looking for:
Name A, 2, 0
Name B, 3, 2
Name C, 3, 5
You can do this with a correlated subquery:
By the way, the order by should always go in the outermost query (unless you are using some sort of limit or top to get a subset of rows).