Skip to content

Commit c9c53fd

Browse files
more updates
1 parent d069735 commit c9c53fd

File tree

8 files changed

+65
-20
lines changed

8 files changed

+65
-20
lines changed

learn-dbt/macros/group_by.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{% macro group_by(n) %}
2+
GROUP BY
3+
{% for i in range(1, n + 1) %}
4+
{{ i }}
5+
{% if not loop.last %} , {% endif %}
6+
{% endfor %}
7+
{% endmacro %}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{% macro rename_segments(column_name) %}
2+
CASE
3+
WHEN {{column_name}} in ('BUILDING', 'HOUSEHOLD', 'FURNITURE')
4+
THEN 'segment_1'
5+
ELSE 'segment_2'
6+
END
7+
{% endmacro %}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{% macro suspend(warehouse_name) %}
2+
3+
{% set sql %}
4+
alter warehouse {{warehouse_name}} suspend
5+
{% endset %}
6+
7+
{% set table = run_query(sql) %}
8+
{% do table.print_table() %}
9+
10+
{% endmacro %}
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1+
with orders as (
2+
SELECT *
3+
FROM {{source('sample', 'orders')}}
4+
5+
)
6+
17
select distinct
28
o_orderdate,
39
sum(o_totalprice) over (order by o_orderdate) as cumulative_sales
410

5-
from "SNOWFLAKE_SAMPLE_DATA"."TPCH_SF1"."ORDERS"
11+
from orders
12+
13+
{% if target.name == 'dev' %}
14+
where year(o_orderdate) = 1996
15+
{% endif %}
616

717
order by o_orderdate

learn-dbt/models/example/my_first_dbt_model.sql

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
11

2-
/*
3-
Welcome to your first dbt model!
4-
Did you know that you can also configure models directly within SQL files?
5-
This will override configurations stated in dbt_project.yml
6-
7-
Try changing "table" to "view" below
8-
*/
9-
{{ config(materialized='table', alias='first_model') }}
2+
{{ config(materialized='table', alias='first_model', tags=["nightly", "example"] ) }}
103

114
with source_data as (
125

13-
select 1 as id, 'NY' as state, '2020-02-01 00:01:00.000'::timestamp as updated_at
6+
select 1 as id, 'NJ' as state, '2020-02-01 00:01:00.000'::timestamp as updated_at
147
union all
158
select null as id, 'CT' as state, '2020-01-01 00:00:00.000'::timestamp as updated_at
169
union all
1710
select 4 as id, 'VT' as state, '2020-01-01 00:00:00.000'::timestamp as updated_at
18-
union all
19-
select 5 as id, 'NJ' as state, '2020-01-01 00:00:00.000'::timestamp as updated_at
2011

2112
)
2213

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
1-
select *
2-
from "SNOWFLAKE_SAMPLE_DATA"."TPCH_SF001"."CUSTOMER"
1+
with sample_customer as (
2+
SELECT *
3+
FROM {{ source('sample', 'customer') }}
4+
)
5+
6+
select
7+
c_custkey,
8+
c_mktsegment,
9+
{{rename_segments('c_mktsegment')}} mkt_segment_adjusted
10+
from sample_customer

learn-dbt/models/example/schema.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,9 @@ sources:
4646
schema: tpch_sf1
4747
tables:
4848
- name: customer
49+
columns:
50+
- name: c_custkey
51+
tests:
52+
- unique
53+
- not_null
4954
- name: orders
Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
1+
with sample_customer as (
2+
SELECT *
3+
FROM {{ source('sample', 'customer') }}
4+
),
5+
6+
sample_orders as (
7+
SELECT *
8+
FROM {{ source('sample', 'orders') }}
9+
)
10+
111
SELECT
212
c.c_custkey,
313
c.c_name,
414
c.c_nationkey as nation,
515
sum(o.o_totalprice) as total_order_price
6-
from {{ source('sample', 'customer') }} c
7-
LEFT JOIN {{ source('sample', 'orders') }} o
16+
from sample_customer c
17+
LEFT JOIN sample_orders o
818
ON c.c_custkey = o.o_custkey
919

10-
group by
11-
c.c_custkey,
12-
c.c_name,
13-
c.c_nationkey
20+
{{group_by(3)}}

0 commit comments

Comments
 (0)