first-ever-not-first-in-window
Installation
SKILL.md
First-ever, not first-in-window
"How many entities did the user meet for the first time in this period" is not a question about the period's rows. It is a question about each entity's whole history, asked once per entity:
// adapted — the window is in HAVING, deliberately, and the subquery scans everything
@Query("SELECT COUNT(*) FROM (SELECT entityId FROM event_entity GROUP BY entityId" +
" HAVING MIN(timestamp) BETWEEN :startTimestamp AND :endTimestamp)")
suspend fun getNewEntityCountInRange(startTimestamp: LocalDateTime, endTimestamp: LocalDateTime): Int
The version everybody writes first puts the window one clause higher:
-- wrong: this is "distinct entities in the window", under a different name
SELECT COUNT(DISTINCT entityId) FROM event_entity
WHERE timestamp BETWEEN :start AND :end