mirror of
https://gitlab.com/cief-data/dbt_cloud.git
synced 2026-08-19 12:24:04 +00:00
DBT structure change
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
--IMPORT
|
||||
WITH messages AS (
|
||||
SELECT * FROM {{ ref('stg_crisp__messages') }}
|
||||
ORDER BY conversation_id, created_datetime, message_id
|
||||
),
|
||||
|
||||
conversations AS (
|
||||
SELECT * FROM {{ ref('stg_crisp__conversations') }}
|
||||
),
|
||||
|
||||
--LOGIC
|
||||
mark_first_dialog_message AS (
|
||||
SELECT
|
||||
CASE
|
||||
WHEN LAG(event_type) OVER (ORDER BY conversation_id, created_datetime, message_id) = 'state:resolved'
|
||||
THEN 1
|
||||
ELSE 0
|
||||
END AS first_dialog_message_flag,
|
||||
*
|
||||
|
||||
FROM messages
|
||||
),
|
||||
|
||||
message_with_dialog_id AS (
|
||||
SELECT
|
||||
(SUM(first_dialog_message_flag) OVER (ORDER BY conversation_id, created_datetime, message_id ROWS UNBOUNDED PRECEDING))+1 AS dialog_id,
|
||||
*
|
||||
|
||||
FROM mark_first_dialog_message
|
||||
ORDER BY conversation_id, created_datetime
|
||||
),
|
||||
|
||||
--FINAL
|
||||
message_with_dialog_id_get_user_id AS (
|
||||
SELECT
|
||||
message_with_dialog_id.dialog_id,
|
||||
message_with_dialog_id.message_id,
|
||||
|
||||
message_with_dialog_id.conversation_id,
|
||||
|
||||
CASE
|
||||
WHEN message_with_dialog_id.message_from_side = 'operator' THEN message_with_dialog_id.user_id
|
||||
WHEN message_with_dialog_id.message_from_side = 'user' and conversations.user_id IS NOT NULL THEN conversations.user_id
|
||||
WHEN message_with_dialog_id.message_from_side = 'user' and conversations.user_id IS NULL THEN null
|
||||
ELSE 'ERROR please contact DATA team'
|
||||
END AS user_id,
|
||||
|
||||
message_with_dialog_id.user_name,
|
||||
message_with_dialog_id.message_from_side,
|
||||
|
||||
CASE
|
||||
WHEN message_with_dialog_id.message_from_side = 'user' AND message_with_dialog_id.user_id IS NOT NULL THEN 'user'
|
||||
WHEN message_with_dialog_id.message_from_side = 'operator' AND message_with_dialog_id.user_id IS NOT NULL THEN 'operator'
|
||||
WHEN message_with_dialog_id.message_from_side = 'operator' AND message_with_dialog_id.user_id IS NULL THEN 'bot'
|
||||
ELSE 'ERROR please contact DATA team'
|
||||
END AS message_generated_by,
|
||||
|
||||
message_with_dialog_id.message_generated_platform,
|
||||
message_with_dialog_id.message_delivered_platform,
|
||||
message_with_dialog_id.message_read_platform,
|
||||
message_with_dialog_id.message_format,
|
||||
message_with_dialog_id.event_type,
|
||||
message_with_dialog_id.event_message,
|
||||
message_with_dialog_id.message,
|
||||
message_with_dialog_id.is_edited_message,
|
||||
message_with_dialog_id.attachment_type,
|
||||
message_with_dialog_id.attachment_name,
|
||||
message_with_dialog_id.attachment_url,
|
||||
message_with_dialog_id.created_datetime,
|
||||
'{{ modules.datetime.datetime.now(modules.pytz.timezone("Asia/Kuala_Lumpur")) }}' AS _dbt_ran_datetime
|
||||
|
||||
FROM message_with_dialog_id
|
||||
LEFT JOIN conversations
|
||||
ON (message_with_dialog_id.user_id = conversations.conversation_id) --The user_id will become conversation_id if message_from_side is user
|
||||
|
||||
),
|
||||
|
||||
-- FINAL
|
||||
final__int_crisp__messages_generate_dialog_ids AS (
|
||||
SELECT
|
||||
-- ids
|
||||
dialog_id,
|
||||
message_id,
|
||||
conversation_id,
|
||||
user_id,
|
||||
|
||||
-- dimensions
|
||||
user_name,
|
||||
message_from_side,
|
||||
message_generated_by,
|
||||
message_generated_platform,
|
||||
message_delivered_platform,
|
||||
message_read_platform,
|
||||
message_format,
|
||||
event_type,
|
||||
event_message,
|
||||
message,
|
||||
is_edited_message,
|
||||
attachment_type,
|
||||
attachment_name,
|
||||
attachment_url,
|
||||
|
||||
-- measures
|
||||
|
||||
-- date/times
|
||||
created_datetime,
|
||||
|
||||
-- metadata
|
||||
_dbt_ran_datetime
|
||||
|
||||
FROM message_with_dialog_id_get_user_id
|
||||
)
|
||||
|
||||
SELECT * FROM final__int_crisp__messages_generate_dialog_ids
|
||||
@@ -0,0 +1,101 @@
|
||||
-- IMPORTS
|
||||
WITH bookings AS (
|
||||
SELECT * FROM {{ ref('stg_exchange__bookings') }}
|
||||
),
|
||||
|
||||
currency_rate_logs AS (
|
||||
SELECT * FROM {{ ref('stg_exchange__currency_rate_logs') }}
|
||||
),
|
||||
|
||||
|
||||
-- LOGIC
|
||||
bookings_calculate_value_join_currency_rate_logs AS (
|
||||
|
||||
SELECT
|
||||
bookings.booking_id,
|
||||
bookings.company_id,
|
||||
bookings.user_id,
|
||||
bookings.booking_marking_id,
|
||||
bookings.service_type,
|
||||
bookings.bank_id,
|
||||
bookings.fix_value,
|
||||
bookings.fix_currency_id,
|
||||
|
||||
currency_rate_logs.payment_method AS estimate_payment_method,
|
||||
currency_rate_logs.base_to_quote_currency_exchange_rate AS estimate_base_to_quote_currency_exchange_rate,
|
||||
|
||||
bookings.quote_currency_id,
|
||||
IFF(bookings.quote_currency_id = bookings.fix_currency_id,
|
||||
fix_value,
|
||||
ROUND(fix_value * estimate_base_to_quote_currency_exchange_rate,2)) AS estimate_quote_value,
|
||||
|
||||
bookings.base_currency_id,
|
||||
IFF(bookings.base_currency_id = bookings.fix_currency_id,
|
||||
fix_value,
|
||||
ROUND(fix_value / estimate_base_to_quote_currency_exchange_rate,2)) AS estimate_base_value,
|
||||
|
||||
-- If we have quote & base value, both of them are not contain 1 (MYR), then need to redo this function
|
||||
CASE
|
||||
WHEN (bookings.base_currency_id = '1')
|
||||
THEN ROUND(estimate_base_value,2)
|
||||
WHEN (bookings.quote_currency_id = '1')
|
||||
THEN ROUND(estimate_quote_value,2)
|
||||
ELSE
|
||||
NULL
|
||||
END AS estimate_value_rm,
|
||||
|
||||
bookings.status,
|
||||
bookings.deleted_datetime,
|
||||
bookings.created_datetime,
|
||||
bookings.updated_datetime,
|
||||
'{{ modules.datetime.datetime.now(modules.pytz.timezone("Asia/Kuala_Lumpur")) }}' AS _dbt_ran_datetime
|
||||
|
||||
FROM bookings
|
||||
|
||||
LEFT JOIN currency_rate_logs
|
||||
ON (bookings.created_datetime >= currency_rate_logs.from_datetime)
|
||||
AND (bookings.created_datetime < currency_rate_logs.to_datetime)
|
||||
AND (bookings.service_type = currency_rate_logs.service_type)
|
||||
AND (bookings.quote_currency_id = currency_rate_logs.quote_currency_id)
|
||||
AND (currency_rate_logs.payment_method = 'CASH')
|
||||
),
|
||||
|
||||
|
||||
-- FINAL
|
||||
final__bookings_get_estimate_booking_values AS (
|
||||
|
||||
SELECT
|
||||
-- ids
|
||||
booking_id,
|
||||
booking_marking_id,
|
||||
company_id,
|
||||
user_id,
|
||||
bank_id,
|
||||
fix_currency_id,
|
||||
quote_currency_id,
|
||||
base_currency_id,
|
||||
|
||||
-- dimensions
|
||||
service_type,
|
||||
estimate_payment_method,
|
||||
status,
|
||||
|
||||
-- measures
|
||||
fix_value,
|
||||
estimate_base_to_quote_currency_exchange_rate,
|
||||
estimate_quote_value,
|
||||
estimate_base_value,
|
||||
estimate_value_rm,
|
||||
|
||||
-- date/times
|
||||
deleted_datetime,
|
||||
created_datetime,
|
||||
updated_datetime,
|
||||
|
||||
-- metadata
|
||||
_dbt_ran_datetime
|
||||
|
||||
FROM bookings_calculate_value_join_currency_rate_logs
|
||||
)
|
||||
|
||||
SELECT * FROM final__bookings_get_estimate_booking_values
|
||||
@@ -0,0 +1,95 @@
|
||||
-- Import
|
||||
WITH companies AS (
|
||||
SELECT * FROM {{ ref('stg_exchange__companies') }}
|
||||
),
|
||||
|
||||
segments AS (
|
||||
SELECT * FROM {{ ref('stg_exchange__segments') }}
|
||||
),
|
||||
|
||||
companies_brg_segments AS (
|
||||
SELECT * FROM {{ ref('stg_exchange__companies_brg_segments') }}
|
||||
),
|
||||
|
||||
|
||||
-- LOGIC
|
||||
get_is_migrated_companies AS (
|
||||
SELECT
|
||||
segment_id,
|
||||
company_id
|
||||
|
||||
FROM companies_brg_segments
|
||||
|
||||
WHERE
|
||||
segment_id = 5
|
||||
),
|
||||
|
||||
|
||||
-- Because we do not have timestamp to get maximum segment_id
|
||||
-- Will act like a id from in asc order, and add in the companies_brg_segments table
|
||||
get_segment_row_created_sequences AS (
|
||||
SELECT
|
||||
*,
|
||||
ROW_NUMBER() OVER (ORDER BY NULL) AS row_created_sequence
|
||||
|
||||
FROM companies_brg_segments
|
||||
|
||||
WHERE
|
||||
segment_id IN (1,2,3,4)
|
||||
AND
|
||||
dbt_valid_to_datetime IS NULL
|
||||
),
|
||||
|
||||
-- use the asc order id in the get_segment_row_created_sequences, and find out the company latest segment
|
||||
get_company_latest_segments AS (
|
||||
SELECT
|
||||
*,
|
||||
row_number() OVER
|
||||
(PARTITION BY company_id
|
||||
ORDER BY row_created_sequence DESC) AS is_latest_segment
|
||||
|
||||
FROM get_segment_row_created_sequences
|
||||
|
||||
QUALIFY
|
||||
is_latest_segment = 1
|
||||
),
|
||||
|
||||
|
||||
-- FINAL
|
||||
final__int_exchange__companies_join_segments AS (
|
||||
SELECT
|
||||
-- ids
|
||||
companies.company_id,
|
||||
companies.company_marking_id,
|
||||
companies.autocount_id,
|
||||
|
||||
-- dimensions
|
||||
companies.name,
|
||||
companies.company_type,
|
||||
companies.business_type,
|
||||
companies.status,
|
||||
segments.name AS exchange_rate_segment,
|
||||
IFF(get_is_migrated_companies.company_id IS NOT NULL, 1, 0) AS is_migrated_company,
|
||||
|
||||
-- measures
|
||||
|
||||
-- date/times
|
||||
companies.created_datetime,
|
||||
companies.updated_datetime,
|
||||
|
||||
-- metadata
|
||||
'{{ modules.datetime.datetime.now(modules.pytz.timezone("Asia/Kuala_Lumpur")) }}' AS _dbt_ran_datetime
|
||||
|
||||
FROM companies
|
||||
|
||||
LEFT JOIN get_company_latest_segments
|
||||
ON (companies.company_id = get_company_latest_segments.company_id)
|
||||
|
||||
LEFT JOIN segments
|
||||
ON (get_company_latest_segments.segment_id = segments.segment_id)
|
||||
|
||||
LEFT JOIN get_is_migrated_companies
|
||||
ON (companies.company_id = get_is_migrated_companies.company_id)
|
||||
)
|
||||
|
||||
SELECT * FROM final__int_exchange__companies_join_segments
|
||||
@@ -0,0 +1,61 @@
|
||||
-- IMPORTS
|
||||
WITH transaction_orders AS (
|
||||
SELECT * FROM {{ ref('stg_exchange__transaction_orders') }}
|
||||
),
|
||||
|
||||
bookings AS (
|
||||
SELECT * FROM {{ ref('stg_exchange__bookings') }}
|
||||
),
|
||||
|
||||
-- LOGICS
|
||||
transaction_orders_join_bookings AS (
|
||||
SELECT
|
||||
transaction_orders.*,
|
||||
bookings.user_id
|
||||
|
||||
FROM transaction_orders
|
||||
|
||||
LEFT JOIN bookings
|
||||
ON (transaction_orders.booking_id = bookings.booking_id)
|
||||
),
|
||||
|
||||
|
||||
-- FINAL
|
||||
final__int_exchange__transaction_orders_get_user_ids AS (
|
||||
SELECT
|
||||
-- ids
|
||||
transaction_order_id,
|
||||
booking_id,
|
||||
company_id,
|
||||
user_id,
|
||||
bank_id,
|
||||
base_currency_id,
|
||||
quote_currency_id,
|
||||
|
||||
-- dimensions
|
||||
transaction_type,
|
||||
payment_method,
|
||||
status,
|
||||
payment_reference,
|
||||
bill_number,
|
||||
|
||||
-- measures
|
||||
base_value,
|
||||
quote_value,
|
||||
base_to_quote_currency_exchange_rate,
|
||||
base_tax,
|
||||
base_service_charge,
|
||||
|
||||
-- date/times
|
||||
expired_datetime,
|
||||
deleted_datetime,
|
||||
created_datetime,
|
||||
updated_datetime,
|
||||
|
||||
-- metadata
|
||||
'{{ modules.datetime.datetime.now(modules.pytz.timezone("Asia/Kuala_Lumpur")) }}' AS _dbt_ran_datetime
|
||||
|
||||
FROM transaction_orders_join_bookings
|
||||
)
|
||||
|
||||
SELECT * FROM final__int_exchange__transaction_orders_get_user_ids
|
||||
Reference in New Issue
Block a user