subquery to CTE
This commit is contained in:
parent
487f6701e3
commit
883618add1
1 changed files with 16 additions and 15 deletions
|
|
@ -1,5 +1,20 @@
|
||||||
with
|
with
|
||||||
raw_bank_transactions as (select * from {{ source("xero", "bank_transactions") }}),
|
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 (
|
stg_xero__bank_transactions as (
|
||||||
select
|
select
|
||||||
{{ adapter.quote("BankTransactionID") }} as id_bank_transaction,
|
{{ adapter.quote("BankTransactionID") }} as id_bank_transaction,
|
||||||
|
|
@ -39,21 +54,7 @@ with
|
||||||
{{ adapter.quote("_airbyte_extracted_at") }} as dwh_extracted_at_utc
|
{{ adapter.quote("_airbyte_extracted_at") }} as dwh_extracted_at_utc
|
||||||
from raw_bank_transactions bt
|
from raw_bank_transactions bt
|
||||||
left join
|
left join
|
||||||
(
|
transaction_signs ts
|
||||||
-- 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
|
|
||||||
on bt.{{ adapter.quote("BankTransactionID") }} = ts.id_bank_transaction
|
on bt.{{ adapter.quote("BankTransactionID") }} = ts.id_bank_transaction
|
||||||
)
|
)
|
||||||
select *
|
select *
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue