-- IMPORTS WITH companies AS ( SELECT * FROM {{ ref('int_exchange__companies_level_segment') }} ), company_addresses AS ( SELECT * FROM {{ ref('stg_exchange__company_addresses') }} ), current_wallets AS ( SELECT * FROM {{ ref('stg_exchange__current_wallets') }} ), companies_and_users_relations AS ( SELECT * FROM {{ ref('stg_exchange__companies_and_users_relations') }} ), company_contacts AS ( SELECT * FROM {{ ref('int_exchange__latest_company_contacts') }} ), documents AS ( SELECT * FROM {{ ref('stg_exchange__documents') }} ), -- LOGICS latest_billing_company_addresses AS ( SELECT *, -- 1 will be latest updated address, rank by updated_at date row_number() OVER (PARTITION BY company_addresses.company_id ORDER BY company_addresses.updated_at DESC) AS lastest_address_rank FROM company_addresses WHERE is_billing_address = 1 QUALIFY lastest_address_rank = 1 ), company_current_wallets AS ( SELECT id, owner_id AS company_id, amount, currency_id, currency_name, created_at, updated_at FROM current_wallets WHERE owner_type = 'App\\Models\\Company' ), count_company_user AS ( SELECT company_id, COUNT(user_id) AS count_user FROM companies_and_users_relations GROUP BY company_id ), latest_company_upload_identity_documents AS ( SELECT id, owner_type, owner_id AS company_id, document_type AS identity_document_type_name, reference AS identity_number, status, status_name, approved_at, created_at, row_number() OVER (PARTITION BY company_id ORDER BY created_at DESC) AS latest_row_number FROM documents WHERE owner_type = 'App\\Models\\Company' AND document_type IN ('IDENTITY_CARD','SSM_REGISTRATION') AND status = '2' QUALIFY latest_row_number = '1' ), -- FINAL final__dim_exchange__companies AS ( SELECT companies.id, companies.name, companies.marking_id, companies.account_software_id, companies.company_type, companies.company_type_name, companies.business_type, companies.business_type_name, companies.segment_id, companies.segment_name, companies.is_migrated_company, companies.status, companies.status_name, companies.created_at, company_contacts.reference AS company_contact_reference, company_contacts.phone AS company_contact_phone, company_contacts.email AS company_contact_email, company_contacts.wechat_id AS company_contact_wechat_id, latest_billing_company_addresses.id AS address_id, latest_billing_company_addresses.country_id, latest_billing_company_addresses.country_name, latest_billing_company_addresses.country_short_code, latest_billing_company_addresses.state_id, latest_billing_company_addresses.state_name, latest_billing_company_addresses.district_id, latest_billing_company_addresses.district_name, latest_billing_company_addresses.postcode, latest_billing_company_addresses.address_line_one, latest_billing_company_addresses.address_line_two, latest_billing_company_addresses.is_billing_address, latest_billing_company_addresses.deleted_at AS address_deleted_at, latest_billing_company_addresses.created_at AS address_created_at, latest_billing_company_addresses.updated_at AS address_updated_at, company_current_wallets.id AS wallet_id, IFF(company_current_wallets.id IS NOT NULL, 1, 0) AS have_wallet, company_current_wallets.amount AS wallet_balance, company_current_wallets.currency_id AS wallet_currency_id, company_current_wallets.currency_name AS wallet_currency_name, company_current_wallets.created_at AS wallet_created_at, company_current_wallets.updated_at AS wallet_updated_at, latest_company_upload_identity_documents.identity_document_type_name, latest_company_upload_identity_documents.identity_number, latest_company_upload_identity_documents.status AS identity_status, latest_company_upload_identity_documents.status_name AS identity_status_name, latest_company_upload_identity_documents.approved_at AS identity_approved_datetime, latest_company_upload_identity_documents.created_at AS identity_created_datetime, count_company_user.count_user AS number_of_user FROM companies LEFT JOIN latest_billing_company_addresses ON (companies.id = latest_billing_company_addresses.company_id) LEFT JOIN company_current_wallets ON (companies.id = company_current_wallets.company_id) LEFT JOIN count_company_user ON (companies.id = count_company_user.company_id) LEFT JOIN company_contacts ON (companies.id = company_contacts.company_id) LEFT JOIN latest_company_upload_identity_documents ON (companies.id = latest_company_upload_identity_documents.company_id) ) SELECT * FROM final__dim_exchange__companies