40 lines
1019 B
SQL
40 lines
1019 B
SQL
{{ config(materialized='table') }}
|
|
|
|
WITH unnested_materials AS (
|
|
SELECT
|
|
primary_category,
|
|
unnest(materials) unnested_material
|
|
FROM {{ source('products', 'products') }}
|
|
),
|
|
|
|
material_scores AS (
|
|
SELECT
|
|
primary_category,
|
|
AVG(lookup.score) AS score
|
|
FROM unnested_materials
|
|
JOIN {{ ref('material_lookup') }} AS lookup
|
|
ON unnested_materials.unnested_material = lookup.material
|
|
GROUP BY primary_category
|
|
),
|
|
|
|
scores AS (
|
|
SELECT
|
|
tcin,
|
|
material_scores.score AS material_score,
|
|
weight * 0.75 AS weight_score,
|
|
packaging * 0.6 AS packaging_score,
|
|
lookup.score AS origin_score
|
|
FROM {{ source('products', 'products') }} AS products
|
|
LEFT JOIN material_scores USING (primary_category)
|
|
LEFT JOIN {{ ref('origin_lookup') }} AS lookup USING (origin)
|
|
)
|
|
|
|
SELECT
|
|
tcin,
|
|
material_score,
|
|
weight_score,
|
|
packaging_score,
|
|
origin_score,
|
|
material_score + weight_score + packaging_score + origin_score AS score
|
|
FROM scores
|