mdrobeulislam1251-ux/t-ex-llm-runpack/sql-analytics

Claude Code and agent skill by mdrobeulislam1251-ux/t-ex-llm-runpack

Trust tier 2
  • community
LLM review pending
Source check pending

Checking that the SKILL.md file is still in the repository.

Use for analytical SQL — cohort retention, funnels, sessionization, dedup, running totals, period-over-period deltas — and the NULL/join/window traps that silently corrupt results. Dialect is probed first; patterns are portable SQL with Postgres flavor noted.

Install

npx vskill@latest install mdrobeulislam1251-ux/t-ex-llm-runpack/sql-analytics

SKILL.md

The same SKILL.md was published earlier as mdrobeulislam1251-ux/t-ex-llm-runpack/sql-analytics.

SQL Analytics

Correctness first: an analytical query that LOOKS right and returns plausible numbers is the most dangerous artifact in data work. Every pattern here ships with its verification check — run it before the numbers leave your hands.

Probe the dialect first

SELECT version(); (Postgres/MySQL) · SELECT @@version; (SQL Server) · SELECT sqlite_version(); · ClickHouse SELECT version();. Date functions, window-frame defaults, and division semantics differ — confirm before pasting patterns across engines.

Dedup — find, inspect, remove

sql
-- find
SELECT natural_key, count(*) FROM t GROUP BY natural_key HAVING count(*) > 1;
-- remove, keep latest (Postgres)
DELETE FROM t a USING t b
WHERE a.natural_key = b.natural_key
AND (a.updated_at, a.id) < (b.updated_at, b.id);

Portable alternative: row_number() in a CTE, delete rn > 1 via the PK. Verify after: the find query returns 0 rows.

Sessionization (gap rule)

sql
WITH gaps AS (
SELECT user_id, event_at,
CASE WHEN lag(event_at) OVER w IS NULL
OR event_at - lag(event_at) OVER w > interval '30 minutes'
THEN 1 ELSE 0 END AS new_session
FROM events
WINDOW w AS (PARTITION BY user_id ORDER BY event_at)
), numbered AS (
SELECT *, sum(new_session) OVER (PARTITION BY user_id ORDER BY event_at
ROWS UNBOUNDED PRECEDING) AS session_id
FROM gaps
)
SELECT user_id, session_id,
min(event_at) AS started, max(event_at) AS ended, count(*) AS events
FROM numbered GROUP BY 1, 2;

30 minutes is the web-analytics convention — state the gap you chose next to the numbers you report.

Cohort retention

sql
WITH cohorts AS (
SELECT user_id, date_trunc('month', min(created_at)) AS cohort_month
FROM users GROUP BY 1
), activity AS (
SELECT DISTINCT user_id, date_trunc('month', event_at) AS active_month
FROM events
)
SELECT c.cohort_month,
(extract(year FROM a.active_month) - extract(year FROM c.cohort_month)) * 12
+ extract(month FROM a.active_month) - extract(month FROM c.cohort_month) AS month_n,
count(DISTINCT a.user_id) AS active_users
FROM cohorts c
JOIN activity a USING (user_id)
WHERE a.active_month >= c.cohort_month
GROUP BY 1, 2 ORDER BY 1, 2;

Verify: each cohort's month_n = 0 row must equal that cohort's size. If it doesn't, the join or the cohort definition is wrong — stop.

Funnel (order-enforced steps)

sql
WITH s1 AS (
SELECT user_id, min(event_at) AS t1 FROM events WHERE name = 'signup' GROUP BY 1
), s2 AS (
SELECT e.user_id, min(e.event_at) AS t2
FROM events e JOIN s1 ON s1.user_id = e.user_id AND e.event_at > s1.t1
WHERE e.name = 'activated' GROUP BY 1
), s3 AS (
SELECT e.user_id, min(e.event_at) AS t3
FROM events e JOIN s2 ON s2.user_id = e.user_id AND e.event_at > s2.t2
WHERE e.name = 'purchased' GROUP BY 1
)
SELECT (SELECT count(*) FROM s1) AS signed_up,
(SELECT count(*) FROM s2) AS activated,
(SELECT count(*) FROM s3) AS purchased;

The e.event_at > s_prev.t_prev join condition is the load-bearing part — it enforces step ORDER. Verify: counts must be monotonically non-increasing; a later step exceeding an earlier one means the ordering constraint is missing.

Period over period

sql
WITH m AS (
SELECT date_trunc('month', created_at) AS month, count(*) AS n
FROM orders GROUP BY 1
)
SELECT month, n,
lag(n) OVER (ORDER BY month) AS prev_n,
round(100.0 * (n - lag(n) OVER (ORDER BY month))
/ NULLIF(lag(n) OVER (ORDER BY month), 0), 1) AS pct_change
FROM m ORDER BY month;

NULLIF(..., 0) makes division-by-zero yield NULL instead of an error; 100.0 (not 100) forces float math — integer division silently floors to 0.

Running totals — the frame trap

With ORDER BY, the default window frame is RANGE, which lumps tied order keys together. Running totals want ROWS:

sql
sum(amount) OVER (ORDER BY created_at, id ROWS UNBOUNDED PRECEDING)

The silent-corruption traps

TrapSymptomFix
NOT IN (subquery) where the subquery can return NULLZero rows, no errorNOT EXISTS, or filter IS NOT NULL inside
Join fanoutMetrics inflate after adding a joinCompare count(*) before/after; pre-aggregate the many-side. SELECT DISTINCT masks the symptom, never fixes it
count(col) vs count(*)Counts mysteriously lowcount(col) skips NULLs — use deliberately or not at all
avg(col) with NULLsAverage too highDecide: coalesce(col, 0) or document the exclusion
BETWEEN on timestampsBoundary rows counted twice across periodsHalf-open: >= start AND < end
GROUP BY 1 ordinals in saved queriesSilent regroup when columns reorderColumn names in anything that persists
Mixed time zonesDaily numbers disagree with the dashboardOne declared zone: date_trunc('day', ts AT TIME ZONE 'UTC')
Integer divisionEvery ratio is 0Multiply by 100.0, NULLIF the denominator

Reconciliation gates — before ANY numbers ship

  1. Parts = whole: segment counts sum to the unsegmented total.
  2. Funnel monotonic: each step ≤ the previous.
  3. Cohort diagonal: month-0 actives = cohort size.
  4. Spot-check 3 rows end-to-end by hand against raw data.
  5. One aggregate vs a known-true source (billing, the production dashboard).

All five pass → ship. Anything fails → the query is wrong until proven otherwise. Plausible ≠ verified.

Anti-patterns

  • SELECT DISTINCT to "fix" inflation (see fanout trap — diagnose the join).
  • OFFSET beyond ~10k rows for exports — keyset pagination instead (postgres-patterns).
  • Correlated subquery per row where one window pass does it.
  • Magic date literals scattered through a query — one params CTE at the top, referenced everywhere.

Security

  • Security scanPassed, 100 of 100Pattern checks on the skill's files.
  • Model reviewNot runA model review is scheduled for the next rescan.

Quality evaluation

  • Task evaluationNot runNo evaluation yet. Run one in Skill Studio.

Source

Popularity

Repository stars
1
7-day trend score
0

Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.

Works with

Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.

  • Claude Code
  • Cursor
  • GitHub Copilot
  • Windsurf
  • Codex
  • Gemini CLI
  • Cline
  • Amp
  • Roo Code
  • Goose
  • Continue
  • Kilo Code
  • Junie
  • Trae
  • OpenHands
  • Qwen Code
  • AI Agent
  • Tabnine Chat
  • Sourcegraph Cody
  • Amazon Q
  • Firebase Genkit
  • Sweep
  • Devin
  • Phind
  • Mentat
  • Claude Desktop
  • Aider
  • GPT Pilot
  • Plandex
  • bolt.new
  • v0
  • Replit Agent
  • Anse
  • TypingMind
  • BetterChatGPT
  • LibreChat
  • Warp AI
  • Pieces
  • Fig AI
Embed badge
vskill VERIFIED badge
[![vskill VERIFIED](https://verified-skill.com/api/v1/skills/mdrobeulislam1251-ux/t-ex-llm-runpack/sql-analytics/badge)](https://verified-skill.com/skills/mdrobeulislam1251-ux/t-ex-llm-runpack/sql-analytics)

Something wrong with this skill or its page? Tell us.