DBT Test: Check Trader Groups Gold Vietnam Trade Coffee 2020 Exporter Group Name 2020
DBT test name: check_trader_groups_gold_vietnam_trade_coffee_2020_exporter_group_name__2020
DBT details
-
Kind:
generic(check_trader_groups) -
Test file: trase/data_pipeline/models/vietnam/trade/bol/coffee/2020/gold/_schema.yml
Description
No description
Details
{{ test_check_trader_groups(**_dbt_generic_test_kwargs) }}{{ config(alias="check_trader_groups_gold_vietn_accdc4f61f0550de4a52887e67a04e1b") }}
WITH trader_groups AS (
SELECT
UNNEST(groups) AS company_group
FROM "trase_production"."main"."postgres_traders"
),
flattened_groups AS (
SELECT
json_extract_string(company_group, '$.group')::VARCHAR AS group_name,
CASE
WHEN json_extract_string(company_group, '$.time_start') = 'null' THEN NULL
ELSE json_extract_string(company_group, '$.time_start')::TIMESTAMP
END AS time_start,
CASE
WHEN json_extract_string(company_group, '$.time_end') = 'null' THEN NULL
ELSE json_extract_string(company_group, '$.time_end')::TIMESTAMP
END AS time_end
FROM
trader_groups
),
model_groups AS (
SELECT
DISTINCT exporter_group_name AS group_name
FROM "trase_production"."main_vietnam_coffee"."gold_vietnam_trade_coffee_2020"
),
matching_groups AS (
SELECT
model.group_name AS model_group_name,
fg.group_name AS official_group_name
FROM model_groups AS model
LEFT JOIN
flattened_groups AS fg
ON model.group_name = fg.group_name
WHERE
( fg.time_start IS NULL AND fg.time_end IS NULL )
OR
( YEAR(fg.time_start) <= 2020
AND ( YEAR(fg.time_end) >= 2020 OR YEAR(fg.time_end) IS NULL ) )
)
SELECT model_group_name AS unmatched_group_name
FROM matching_groups
WHERE
official_group_name IS NULL
OR model_group_name IS NULL
OR LEN(model_group_name) = 0