use date field instead of breaking things down

This commit is contained in:
Pablo Martin 2024-05-23 16:13:53 +02:00
parent b0528ec2c0
commit 65f5b7377a

View file

@ -12,8 +12,9 @@ with
xero__credit_notes as (select * from {{ ref("xero__credit_notes") }}), xero__credit_notes as (select * from {{ ref("xero__credit_notes") }}),
fees_invoiced as ( fees_invoiced as (
select select
extract(year from i.invoice_issued_date_utc) as invoice_issued_year, cast(
extract(month from i.invoice_issued_date_utc) as invoice_issued_month, date_trunc('month', i.invoice_issued_date_utc) as date
) as invoice_issued_year_month,
case case
when ili.item_code in {{ booking_fee_items }} when ili.item_code in {{ booking_fee_items }}
then 'booking_fees' then 'booking_fees'
@ -34,8 +35,7 @@ with
or ili.item_code in {{ verification_fee_items }} or ili.item_code in {{ verification_fee_items }}
) )
group by group by
extract(year from i.invoice_issued_date_utc), date_trunc('month', i.invoice_issued_date_utc),
extract(month from i.invoice_issued_date_utc),
case case
when ili.item_code in {{ booking_fee_items }} when ili.item_code in {{ booking_fee_items }}
then 'booking_fees' then 'booking_fees'
@ -48,12 +48,9 @@ with
), ),
fees_credited as ( fees_credited as (
select select
extract( cast(
year from cn.credit_note_issued_date_utc date_trunc('month', cn.credit_note_issued_date_utc) as date
) as credit_note_issued_year, ) as credit_note_issued_year_month,
extract(
month from cn.credit_note_issued_date_utc
) as credit_note_issued_month,
case case
when cnli.item_code in {{ booking_fee_items }} when cnli.item_code in {{ booking_fee_items }}
then 'booking_fees' then 'booking_fees'
@ -75,8 +72,7 @@ with
or cnli.item_code in {{ verification_fee_items }} or cnli.item_code in {{ verification_fee_items }}
) )
group by group by
extract(year from cn.credit_note_issued_date_utc), date_trunc('month', cn.credit_note_issued_date_utc),
extract(month from cn.credit_note_issued_date_utc),
case case
when cnli.item_code in {{ booking_fee_items }} when cnli.item_code in {{ booking_fee_items }}
then 'booking_fees' then 'booking_fees'
@ -88,8 +84,7 @@ with
end end
) )
select select
i.invoice_issued_year as issued_year, i.invoice_issued_year_month as issued_year_month,
i.invoice_issued_month as issued_month,
i.fee_category, i.fee_category,
coalesce(i.fees_invoiced, 0) as fees_invoiced_in_gbp, coalesce(i.fees_invoiced, 0) as fees_invoiced_in_gbp,
coalesce(c.fees_credited, 0) as fees_credited_in_gbp, coalesce(c.fees_credited, 0) as fees_credited_in_gbp,
@ -97,6 +92,5 @@ select
from fees_invoiced i from fees_invoiced i
left join left join
fees_credited c fees_credited c
on i.invoice_issued_year = c.credit_note_issued_year on i.invoice_issued_year_month = c.credit_note_issued_year_month
and i.invoice_issued_month = c.credit_note_issued_month
and i.fee_category = c.fee_category and i.fee_category = c.fee_category