diff --git a/README.md b/README.md index 94c6e39..0b553b0 100644 --- a/README.md +++ b/README.md @@ -182,11 +182,12 @@ models: This macro generates the SQL for a given model with all references pulled up into import CTEs, which you can then paste back into the model. ### Arguments: -* `model_name` (required): The model table you wish to generate SQL with import CTEs for. +* `model_name` (required): The model you wish to generate SQL with import CTEs for. * `leading_commas` (optional, default = false): Whether you want your commas to be leading (vs trailing). ### Usage: -1. Copy the macro into a statement tab in the dbt Cloud IDE, or into an analysis file, and compile your code. +1. Create a model with your original SQL query +2. Copy the macro into a statement tab in the dbt Cloud IDE, or into an analysis file, and compile your code ``` {{ codegen.generate_model_import_ctes( @@ -200,4 +201,70 @@ Alternatively, call the macro as an [operation](https://docs.getdbt.com/docs/usi $ dbt run-operation generate_model_import_ctes --args '{"model_name": "my_dbt_model"}' ``` -2. Replace the contents of the model's current SQL file with the compiled or logged code \ No newline at end of file +3. The new SQL - with all references pulled up into import CTEs - will be logged to the command line + +``` +with customers as ( + + select * from {{ ref('stg_customers') }} + +), + +orders as ( + + select * from {{ ref('stg_orders') }} + +), + +payments as ( + + select * from {{ ref('stg_payments') }} + +), + +customer_orders as ( + + select + customer_id, + min(order_date) as first_order, + max(order_date) as most_recent_order, + count(order_id) as number_of_orders + from orders + group by customer_id + +), + +customer_payments as ( + + select + orders.customer_id, + sum(amount) as total_amount + from payments + left join orders on + payments.order_id = orders.order_id + group by orders.customer_id + +), + +final as ( + + select + customers.customer_id, + customers.first_name, + customers.last_name, + customer_orders.first_order, + customer_orders.most_recent_order, + customer_orders.number_of_orders, + customer_payments.total_amount as customer_lifetime_value + from customers + left join customer_orders + on customers.customer_id = customer_orders.customer_id + left join customer_payments + on customers.customer_id = customer_payments.customer_id + +) + +select * from final +``` + +4. Replace the contents of the model's current SQL file with the compiled or logged code \ No newline at end of file