From 883618add1a785758653ebd0a18582f975a52185 Mon Sep 17 00:00:00 2001 From: Pablo Martin Date: Mon, 1 Jul 2024 14:02:13 +0200 Subject: [PATCH] subquery to CTE --- .../xero/stg_xero__bank_transactions.sql | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/models/staging/xero/stg_xero__bank_transactions.sql b/models/staging/xero/stg_xero__bank_transactions.sql index 4d1ea9b..9572f6d 100644 --- a/models/staging/xero/stg_xero__bank_transactions.sql +++ b/models/staging/xero/stg_xero__bank_transactions.sql @@ -1,5 +1,20 @@ with raw_bank_transactions as (select * from {{ source("xero", "bank_transactions") }}), + transaction_signs as ( + -- making this tiny CTE to be able to reference the transaction_sign col + -- in the main body easily and avoid plastering the case-when a + -- gazillion times. + select + {{ adapter.quote("BankTransactionID") }} as id_bank_transaction, + case + when {{ adapter.quote("Type") }} like '%SPEND%' + then -1 + when {{ adapter.quote("Type") }} like '%RECEIVE%' + then 1 + else null + end as transaction_sign + from raw_bank_transactions + ), stg_xero__bank_transactions as ( select {{ adapter.quote("BankTransactionID") }} as id_bank_transaction, @@ -39,21 +54,7 @@ with {{ adapter.quote("_airbyte_extracted_at") }} as dwh_extracted_at_utc from raw_bank_transactions bt left join - ( - -- making this tiny subquery to be able to reference the - -- transaction_sign col above easily and avoid plastering the - -- case-when a gazillion times. - select - {{ adapter.quote("BankTransactionID") }} as id_bank_transaction, - case - when {{ adapter.quote("Type") }} like '%SPEND%' - then -1 - when {{ adapter.quote("Type") }} like '%RECEIVE%' - then 1 - else null - end as transaction_sign - from raw_bank_transactions - ) ts + transaction_signs ts on bt.{{ adapter.quote("BankTransactionID") }} = ts.id_bank_transaction ) select *