modular-sql-ctes — independently scanned and version-tracked by SaferSkills.
SaferSkills independently audited modular-sql-ctes (Agent Skill) and scored it 100/100 (green). The audit ran 55 deterministic rules across Security, Supply Chain, Maintenance, Transparency, and Community; it found 0 high-severity and 0 lower-severity findings. The full rule-by-rule trace and per-finding evidence are below. Free, methodology-open.
Findings & checks · 0 flagged
Every scanned point with the score it earned and what moved between them.
First recorded scan — no prior version to compare against.
The primary manifest — the file an agent reads to learn what this artifact does.
Use when transforming a working-but-unreadable SQL query into a maintainable, testable model. Triggers:
Don't use for one-off exploration queries that won't be reused. Refactoring exploratory code is over-engineering.
| Input | Why it matters |
|---|---|
| Current SQL | The query to refactor |
| Target grain | What one row in the final output represents |
| Source tables | Where the data is coming from |
| Warehouse | Snowflake / BigQuery / Postgres / Redshift |
| dbt or raw SQL | Affects model layout |
WHERE clauses in staging where possible to reduce data scanned downstream.inner join, left join, full join. Never bare join. in production layers.** Only allowed in staging if every source column is intentionally used. List columns explicitly in int_ and fct_`.-- =========================================================
-- Model: fct_user_daily_activity
-- Grain: one row per (user_id, activity_date) in UTC
-- Primary key: (user_id, activity_date)
-- Sources:
-- raw.events.product_events
-- raw.users.dim_user
-- Refresh: daily at 06:00 UTC
-- Owner: @sarah.kim
-- =========================================================
with stg_events as (
select
cast(user_id as varchar) as user_id,
lower(event_name) as event_name,
cast(event_at as timestamp_ntz) as event_ts,
cast(event_at as date) as event_date
from raw.events.product_events
where event_at >= '2024-01-01'
and user_id is not null
),
stg_users as (
select
cast(user_id as varchar) as user_id,
country,
plan_tier,
signup_at
from raw.users.dim_user
where is_internal_user = false
),
int_events_enriched as (
select
e.user_id,
e.event_ts,
e.event_date,
e.event_name,
case when e.event_name = 'purchase' then 1 else 0 end as is_purchase,
case when e.event_name = 'session_start' then 1 else 0 end as is_session
from stg_events e
),
int_user_daily as (
select
ee.user_id,
ee.event_date,
count(*) as total_events,
sum(ee.is_purchase) as purchase_count,
sum(ee.is_session) as session_count
from int_events_enriched ee
group by ee.user_id, ee.event_date
),
fct_user_daily_activity as (
select
ud.user_id,
ud.event_date as activity_date,
u.country,
u.plan_tier,
ud.total_events,
ud.purchase_count,
ud.session_count,
case when ud.purchase_count > 0 then 1 else 0 end as did_purchase
from int_user_daily ud
inner join stg_users u using (user_id)
)
select * from fct_user_daily_activity;select * outside stagingstg_ / int_ / fct_ convention1:N table without aggregating first inflates row counts. Add qualify row_number() over ... = 1 or aggregate before joining.WHERE converts the join to inner. Put it in ON instead.sources.yml<model>.ymlunique and not_null on primary key columns{{ ref('stg_users') }} for cross-model dependenciesstg_ as views, int_ and fct_ as tables| Snowflake | BigQuery | Postgres |
|---|---|---|
date_trunc('week', d) | date_trunc(d, week) | date_trunc('week', d) |
datediff('day', a, b) | date_diff(b, a, day) | (b - a) |
dateadd('day', n, d) | date_add(d, interval n day) | d + interval 'n day' |
qualify | qualify | use subquery |
nullif(a, b) | nullif(a, b) | nullif(a, b) |
warehouse-query-optimization — when the refactored query needs to be fastdata-quality-audit — verify the output meets grain/uniqueness expectationsmetric-definition — wrap the final model in a metric spec~30 seconds. Free. No account. Every finding cites a rule and a line of evidence.