Skip to content

Instantly share code, notes, and snippets.

-- 1) summary output below across all tables
with dbt_objects as (
-- get list of tables maintained by dbt
SELECT
t.source_table_full_name,
count(distinct h.statement_id) as dbt_query_count
FROM system.query.history AS h
LEFT JOIN system.access.table_lineage AS t
ON t.statement_id = h.cache_origin_statement_id
@adinsmoor
adinsmoor / xref.sql
Created September 18, 2025 03:58
Example xref custom macro
{% macro xref() %}
-- extract user-provided positional and keyword arguments
{% set version = kwargs.get('version') %}
{% if (varargs | length) == 1 %}
{% set modelname = varargs[0] %}
{% else %}
{% set packagename = varargs[0] %}
{% set modelname = varargs[1] %}
{% endif %}
{%- materialization test, adapter='synapse' -%}
{% set relations = [] %}
{% if should_store_failures() %}
{% set identifier = model['alias'] %}
{% set old_relation = adapter.get_relation(database=database, schema=schema, identifier=identifier) %}
{% set store_failures_as = config.get('store_failures_as') %}
@adinsmoor
adinsmoor / test.sql
Created August 11, 2025 10:15
Synapse "test" fix
{%- materialization test, adapter='fabric' -%}
{% set relations = [] %}
{% set limit = config.get('limit') %}
{% set fail_calc = config.get('fail_calc') %}
{% set warn_if = config.get('warn_if') %}
{% set error_if = config.get('error_if') %}
{% set number_of_errors = 0 %}
{% if should_store_failures() %}
{{
config(
materialized='table'
)
}}
with
source as (
{{ config(
tags=["finance"]
) }}
{% macro centralize_test_failures(results) %}
{# --add "{{ centralize_test_failures(results) }}" to an on-run-end: block in dbt_project.yml #}
{# --run with dbt build --store-failures. #}
{%- set test_results = [] -%}
{%- for result in results -%}
@adinsmoor
adinsmoor / dbt_workshop_solution_ex1.sql
Created March 25, 2024 23:12
dbt Cloud Workshop: Mesh Solution Code
with campaigns as (
select * from {{ ref('stg_campaigns') }}
),
campaign_sequence as (
select * from {{ ref('stg_campaign_sequence') }}
),
final as (
select
campaign_sequence.campaign_id,
@adinsmoor
adinsmoor / _loading_source.yml
Last active April 29, 2023 06:54
autogen macro example
version: 2
sources:
- name: csv_load
database: adinsmoor_sandbox_dev
schema: loading_layer
tables:
- name: sw_line_input
- name: recipe
@adinsmoor
adinsmoor / dbt_nz_lab_starter_code.sql
Last active December 14, 2022 00:01
Starter code for hands-on exercises
-- Exercise 1: Define a staging model (stg_player.sql)
with player as (
select * from {{ source('fifa', 'player') }}
)
select
id as player_id
, affiliation_id
, concat(player_first_name, ' ', player_last_name) as player_name
@adinsmoor
adinsmoor / dbt_nz_lab_solution_code.sql
Last active December 12, 2023 13:36
Solution code for hands-on exercises
-- Exercise 1: Define a staging model (stg_player.sql)
with player as (
select * from {{ source('fifa', 'player') }}
)
select
id as player_id
, affiliation_id
, concat(player_first_name, ' ', player_last_name) as player_name