- How do I count a streak of consecutive days in SQL?
- Ask in plain English — 'each user's longest streak of consecutive days with an order.' nlqdb compiles the gaps-and-islands query: it subtracts a per-user `ROW_NUMBER()` ordered by day from the date, which turns every unbroken run into one constant, then groups by that constant and counts the days. It runs the query in Postgres and shows the SQL so you can confirm a gap breaks the run.
- Why doesn't COUNT(DISTINCT day) give me the streak?
- Because `COUNT(DISTINCT day)` counts total active days, not the length of the longest unbroken run. A user active on the 1st, 2nd, and 5th has three active days but a longest streak of two. The streak needs the gaps-and-islands trick — grouping consecutive days into runs — not a plain distinct count.
- What is the gaps-and-islands pattern?
- It's the standard SQL technique for finding runs of consecutive rows. You subtract a `ROW_NUMBER()` (ordered by the sequence column) from that column: within a consecutive run the difference is constant, and any gap shifts it, so each run — each 'island' — gets a unique key you can group by. It answers streaks, uptime windows, and consecutive-number ranges.
- Can I compute a streak on a Postgres database I already run?
- Yes — connect it with the signed-in BYO connect verb (`nlq db connect`, `SK-DBCONN-001`; see /solve/query-existing-postgres-in-natural-language) and ask for the streak in place, no ETL into a separate store. The honest limits: BYO connect is signed-in only (not the public embed), and the answer is read-only — nlqdb reports the streak, it doesn't materialise it into a column.