From a70c301e38fe3369f2046dce359ed36e80a6ef2b Mon Sep 17 00:00:00 2001 From: Horace Liu <65971326+horaceliuHL@users.noreply.github.com> Date: Fri, 8 Nov 2024 11:21:54 -0500 Subject: [PATCH] Add 10Q dbt models (#593) --- models/projects/equities/ez_sec_gov_10q.sql | 11 +++++++ .../staging/equities/__equities__source.yml | 6 ++++ models/staging/equities/fact_sec_gov_10q.sql | 23 +++++++++++++ .../equities/fact_sec_gov_10q_pivot.sql | 32 +++++++++++++++++++ 4 files changed, 72 insertions(+) create mode 100644 models/projects/equities/ez_sec_gov_10q.sql create mode 100644 models/staging/equities/__equities__source.yml create mode 100644 models/staging/equities/fact_sec_gov_10q.sql create mode 100644 models/staging/equities/fact_sec_gov_10q_pivot.sql diff --git a/models/projects/equities/ez_sec_gov_10q.sql b/models/projects/equities/ez_sec_gov_10q.sql new file mode 100644 index 00000000..9bc43d14 --- /dev/null +++ b/models/projects/equities/ez_sec_gov_10q.sql @@ -0,0 +1,11 @@ +{{ + config( + materialized="table", + snowflake_warehouse="X_SMALL", + database="equities", + schema="core", + alias="ez_sec_metrics", + ) +}} + +select * from {{ ref("fact_sec_gov_10q_pivot") }} diff --git a/models/staging/equities/__equities__source.yml b/models/staging/equities/__equities__source.yml new file mode 100644 index 00000000..e5021f72 --- /dev/null +++ b/models/staging/equities/__equities__source.yml @@ -0,0 +1,6 @@ +sources: + - name: PROD_LANDING + schema: PROD_LANDING + database: LANDING_DATABASE + tables: + - name: equities_metrics diff --git a/models/staging/equities/fact_sec_gov_10q.sql b/models/staging/equities/fact_sec_gov_10q.sql new file mode 100644 index 00000000..767bcb07 --- /dev/null +++ b/models/staging/equities/fact_sec_gov_10q.sql @@ -0,0 +1,23 @@ +{{ config(materialized="table") }} +with + max_extraction as ( + select max(extraction_date) as max_date + from {{ source("PROD_LANDING", "equities_metrics") }} + ), + data as ( + select + value:"cik"::NUMBER AS cik, + value:"adsh"::NUMBER AS adsh, + value:"company_name"::VARCHAR AS company_name, + value:"metric_name"::VARCHAR AS metric_name, + value:"metric_value"::VARCHAR AS metric_value, + value:"parent_metric"::VARCHAR AS parent_metric, + value:"time_period"::VARCHAR AS time_period, + to_date(to_timestamp(value:"date"::NUMBER / 1000)) AS date, + extraction_date + from + {{ source("PROD_LANDING", "equities_metrics") }}, + lateral flatten(input => parse_json(source_json)) + where extraction_date = (select max_date from max_extraction) + ) +select * from data \ No newline at end of file diff --git a/models/staging/equities/fact_sec_gov_10q_pivot.sql b/models/staging/equities/fact_sec_gov_10q_pivot.sql new file mode 100644 index 00000000..d4a679d8 --- /dev/null +++ b/models/staging/equities/fact_sec_gov_10q_pivot.sql @@ -0,0 +1,32 @@ +{{ config(materialized="table") }} + +WITH RECURSIVE path_builder AS ( + SELECT + cik, + adsh, + company_name, + metric_name, + metric_value, + time_period, + date, + ARRAY_CONSTRUCT(metric_name) AS col_path + FROM {{ ref("fact_sec_gov_10q") }} + WHERE parent_metric IS NULL + + UNION ALL + + SELECT + child.cik, + child.adsh, + child.company_name, + child.metric_name, + child.metric_value, + child.time_period, + child.date, + ARRAY_APPEND(parent.col_path, child.metric_name) AS col_path + FROM {{ ref("fact_sec_gov_10q") }} AS child + JOIN path_builder AS parent + ON child.parent_metric = parent.metric_name AND child.adsh = parent.adsh AND child.time_period = parent.time_period +) + +SELECT * FROM path_builder ORDER BY metric_name \ No newline at end of file