Skip to content

Instantly share code, notes, and snippets.

View prmoore77's full-sized avatar

Philip Moore prmoore77

View GitHub Profile
product_name product_id customer_id date_id unit_quantity sales_amount
Hershey Bar 201 Phil 2022-01-01 1.000 3.000
Hershey Bar 201 Lottie 2022-01-02 5.000 15.000
Nerds 202 Kalie 2022-01-02 2.000 5.000
Tomatoes 102 Phil 2022-01-02 2.000 2.000
Spinach 101 Popeye 2022-01-03 10.000 5.000
Spinach 101 Brutus 2022-01-04 1.000 0.500
Spinach 101 Lottie 2022-01-04 1.000 0.500
Spinach 101 Phil 2022-01-05 2.000 2.000
SELECT -- Use LPAD here to indent the node information based upon its depth in the hierarchy
LPAD ('-', (products.ancestor_level_number - 1) * 7, '-')
|| products.ancestor_level_name AS product_level_name
, LPAD ('-', (products.ancestor_level_number - 1) * 7, '-')
|| products.ancestor_node_name AS product_node_name
-- Aggregates
, SUM (facts.sales_amount) AS sum_sales_amount
, SUM (facts.unit_quantity) AS sum_unit_quantity
, COUNT (DISTINCT facts.customer_id) AS distinct_customer_count
, COUNT (*) AS count_of_fact_records
product_level_name product_node_name sum_sales_amount sum_unit_quantity distinct_customer_count count_of_fact_records
Total Products All Products 33.000 24.000 5 8
-------Category -------Candy 23.000 8.000 3 3
--------------UPC --------------Hershey Bar 18.000 6.000 2 2
--------------UPC --------------Nerds 5.000 2.000 1 1
-------Category -------Produce 10.000 16.000 4 5
--------------UPC --------------Spinach 8.000 14.000 4 4
--------------UPC --------------Tomatoes 2.000 2.000 1 1
WITH rollup_aggregations AS (
SELECT CASE WHEN GROUPING (level_3_node_id) = 0
THEN products.level_3_node_id
WHEN GROUPING (level_2_node_id) = 0
THEN products.level_2_node_id
WHEN GROUPING (level_1_node_id) = 0
THEN products.level_1_node_id
END AS product_node_id
-- Aggregates
, SUM (facts.sales_amount) AS sum_sales_amount
product_level_name product_node_name sum_sales_amount sum_unit_quantity distinct_customer_count count_of_fact_records
Total Products All Products 33.000 24.000 5 8
-------Category -------Candy 23.000 8.000 3 3
--------------UPC --------------Hershey Bar 18.000 6.000 2 2
--------------UPC --------------Nerds 5.000 2.000 1 1
-------Category -------Produce 10.000 16.000 3 5
--------------UPC --------------Spinach 8.000 14.000 2 4
--------------UPC --------------Tomatoes 2.000 2.000 1 1
SELECT
-- Product Dimension
LPAD (' ', (products.ancestor_level_number - 1) * 7, ' ')
|| products.ancestor_level_name AS product_level_name
, LPAD (' ', (products.ancestor_level_number - 1) * 7, ' ')
|| products.ancestor_node_name AS product_node_name
-- Geography Dimension
, LPAD (' ', (geographies.ancestor_level_number - 1) * 7, ' ')
|| geographies.ancestor_level_name AS geography_level_name
, LPAD (' ', (geographies.ancestor_level_number - 1) * 7, ' ')
SELECT
-- Product Ancestor Attributes
LPAD ('-', (products.ancestor_level_number - 1) * 7, '-')
|| products.ancestor_level_name AS product_ancestor_level_name
, LPAD ('-', (products.ancestor_level_number - 1) * 7, '-')
|| products.ancestor_node_name AS product_ancestor_node_name
-- Product Descendant Attributes
, LPAD ('-', (products.descendant_level_number - 1) * 7, '-')
|| products.descendant_level_name AS product_descendant_level_name
, LPAD ('-', (products.descendant_level_number - 1) * 7, '-')
product_ancestor_level_name product_ancestor_node_name product_descendant_level_name product_descendant_node_name customer_id sales_amount
Total Products All Products --------------UPC --------------Hershey Bar Lottie 15.000
Total Products All Products --------------UPC --------------Hershey Bar Phil 3.000
Total Products All Products --------------UPC --------------Nerds Kalie 5.000
-------Category -------Candy --------------UPC --------------Hershey Bar Lottie 15.000
-------Category -------Candy --------------UPC --------------Hershey Bar Phil 3.000
-------Category -------Candy --------------UPC --------------Nerds Kalie 5.000
--------------UPC --------------Hershey Bar --------------UPC --------------Hershey Bar Lottie 15.000
--------------UPC --------------Hershey Bar --------------UPC --------------Hershey Bar Phil 3.000
--------------UPC --------------Nerds --------------UPC --------------Nerds Kalie 5.000
SELECT -- Use LPAD here to indent the node information based upon its depth in the hierarchy
LPAD ('-', (products.ancestor_level_number - 1) * 7, '-')
|| products.ancestor_level_name AS product_level_name
, LPAD ('-', (products.ancestor_level_number - 1) * 7, '-')
|| products.ancestor_node_name AS product_node_name
-- Aggregates
, SUM (facts.sales_amount) AS sum_sales_amount
, SUM (facts.unit_quantity) AS sum_unit_quantity
, COUNT (DISTINCT facts.customer_id) AS distinct_customer_count
, COUNT (*) AS count_of_fact_records
product_level_name product_node_name sum_sales_amount sum_unit_quantity distinct_customer_count count_of_fact_records
Total Products All Products 23.000 8.000 3 3
-------Category -------Candy 23.000 8.000 3 3
--------------UPC --------------Hershey Bar 18.000 6.000 2 2
--------------UPC --------------Nerds 5.000 2.000 1 1