Chuyển đến nội dung
Skillpedia
Cơ sở dữ liệu6.2k · #14405

effective-dbt-sql

Skill này làm gì

Skill này đưa ra các quy tắc viết SQL mô hình dbt đúng và dễ đọc. Skill dành cho kỹ sư analytics viết hoặc thay đổi mô hình và chỉ số dbt.

Khi nào nên dùng

  • ✓Khi bạn chuẩn bị viết SQL bên trong một mô hình dbt.
  • ✓Khi bạn muốn thay đổi định nghĩa SQL của một chỉ số.
  • ✓Khi bạn đang cân nhắc có nên thêm một truy vấn con hay tái sử dụng trường có sẵn.

Liên kết skill

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

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/lightdash/lightdash/blob/main/skills/effective-dbt-sql/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Thêm một chỉ số tỷ lệ rời bỏ vào mô hình subscriptions của dbt, tái sử dụng các trường chúng tôi đã có thay vì lặp lại logic.”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Skill yêu cầu agent tái sử dụng trường có sẵn khi có thể, tránh các truy vấn con phụ thuộc vào từng dòng của truy vấn bên ngoài, và ưu tiên CTE cho logic phức tạp. Skill cũng yêu cầu agent theo đúng phong cách đã dùng trong repository.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

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).
Câu hỏi thường gặp

Skill có thay đổi cách đặt tên hoặc định dạng không?

Không. Skill chỉ bao quát ý nghĩa của SQL, không bao quát tên hay cách trình bày.

Skill có viết lại code đang chạy không?

Không. Skill chỉ áp dụng cho SQL mới hoặc SQL đã thay đổi, không viết lại code đang hoạt động.

Truy vấn con tương quan là gì?

Đó là truy vấn con tham chiếu đến các dòng của truy vấn bên ngoài, và skill khuyên tránh loại này.

Chi tiết

Mô tả gốc của tác giả

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

Độ phổ biến

Sao GitHub
6.2k
Vị trí xếp hạng
#14405
Giấy phép
—
Cập nhật
2/11/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự