Dev Helpers

SQL / Upserts and transactions

Accumulate an incoming count

Dev Helpers · September 29, 2026

An upsert can accumulate a new count into an existing key. The uniqueness constraint determines which row receives the update.

Worked example

CREATE TABLE totals(k TEXT PRIMARY KEY,n INTEGER);
INSERT INTO totals VALUES('a',3);
INSERT INTO totals VALUES('a',4) ON CONFLICT(k) DO UPDATE SET n=n+excluded.n;
SELECT n FROM totals;

Result

[
  {
    "n": 7
  }
]

Implementation notes

An increment retried after a timeout can count twice. For externally retried events, keep an event identifier so ingestion can recognize duplicates.

Try it yourself

Apply two increments for the same key and one for another key.

Open the SQL example →

Editorial date September 29, 2026 · Published December 6, 2026

Related articles

Compare two literal lists

Set operations compare rows under SQL's rules for duplicates. Check whether the result represents membership or the number of occurrences.

Count jobs per active day

Grouping timestamps by day produces counts only for days present in the data. A continuous chart may need a calendar series as well.