跳到正文
Skillpedia
数据库6.2k · #14405

effective-dbt-sql

它能做什么

这项技能提供编写正确且易读的 dbt 模型 SQL 的规则。适合编写或修改 dbt 模型和指标的分析工程师。

适用场景

  • ✓当你即将在 dbt 模型中编写 SQL 时。
  • ✓当你想修改某个指标的 SQL 定义时。
  • ✓当你在决定是添加子查询还是复用现有字段的时候。

技能链接

https://github.com/lightdash/lightdash/blob/main/skills/effective-dbt-sql/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/lightdash/lightdash/blob/main/skills/effective-dbt-sql/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请在订阅 dbt 模型中添加一个流失率指标,复用我们已有的字段,而不是重复写相同的逻辑。”

每周 5 个新技能,1 分钟读完。

工作原理

这项技能要求智能体尽可能复用现有字段,避免依赖外层每一行的子查询,并对复杂逻辑优先使用 CTE。它还要求智能体遵循代码仓库中已有的写法风格。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Effective dbt SQL

Write dbt model SQL that is correct, readable, and idiomatic. This skill is about the semantics of the SQL you write — not naming or formatting.

Scope

Use this when you are about to write or change SQL inside a dbt model (.sql), or a metric/dimension whose value is defined by SQL.

This skill covers semantics only. It does not prescribe naming schemes, file layout, or indentation. For those, follow the rule below.

Match the target repo first. These are fallback defaults, not mandates. Before applying any rule, read the models around the one you are changing and match the repo's established conventions where they differ. Apply these rules to new or changed SQL only — do not reformat, rename, or restructure existing code that already works.

The rules

  1. Reuse before you re-derive. If the value you are asked for is already expressed by an existing dimension or metric — or by two of them combined — compose those fields instead of writing new SQL to recompute it. Read the model's existing dimensions/metrics before adding one. See reuse-over-subqueries.

  2. No correlated subqueries. Never compute a value with a subquery that references the outer query's rows (a per-row correlated subquery in SELECT or WHERE). Reuse an existing field, or join to an aggregated CTE instead. This is the single most common mistake. See cte-pipelines.

  3. CTEs over nested subqueries. Structure a query as a pipeline of named CTEs, each referenced by the next, ending in a final select. Prefer this over subqueries nested inside from/where. See cte-pipelines.

  4. Explicit joins. Always state the join type (inner/left/…) and the on condition. Never comma-join. Know the grain: a join that fans out rows silently changes every downstream aggregate.

  5. Explicit columns. Select named columns rather than select * when adding or changing what a model emits, so a change to an upstream model can't silently alter this model's output. (A pass-through select * from final at the very end of a CTE pipeline is fine — the columns are already pinned by the CTEs.)

  6. Don't break references. Before editing a model's SQL, read the source()/ref() models it selects from. Do not rename or remove a column that downstream models or schema.yml reference without checking the impact first.

Not in scope

  • Emitted-type / casting safety (e.g. boolean-vs-numeric column types): follow the warehouse skill the system prompt points you to (/home/user/.lightdash-skills/). This skill does not restate casting rules.
  • Naming and formatting conventions: match the target repo (see the rule above).
常见问题

它会改变命名或格式吗?

不会。技能关注的是 SQL 的含义,而不是名称或排版。

它会重写现有代码吗?

不会。技能只适用于新的或已修改的 SQL,不会重写正常运行的代码。

什么是相关子查询?

它是引用外层查询行的子查询,技能要求避免使用它。

详情

作者的原始描述

Use when writing or modifying dbt model SQL — deciding whether to add a subquery or reuse an existing dimension/metric, structuring a query, joining models, or refactoring a metric's SQL. Encodes SQL semantic-correctness rules: reuse existing fields, prefer CTEs over correlated subqueries, and make joins and column lists explicit.

repo: lightdash/lightdash

热度

GitHub 星标
6.2k
排名
#14405
许可证
—
更新时间
2026/11/2

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能