Merge a Batch Into a Daily Counter

daily holds one row per day with a running views total. A batch of new counts has arrived in batch. Merge it in:

  • a day already in daily has the batch count added to its total
  • a day not yet in daily is inserted
  • batch contains more than one row for the same day, and all of them count

Then return the final table:

SELECT day, views FROM daily ORDER BY day;

Do the merge with one statement — no SELECT to find out which days exist first. Two copies of this running at the same time must not lose a count or fail.

Expected output


day        | views
-----------+------
2024-03-01 | 115
2024-03-02 | 220
2024-03-03 | 30
2024-03-04 | 40
PostgreSQL

Press Run to start the SQL engine.

SensAI
Beta
Tap the mic to stop voice mode