Settimana 71 - Dati Semi-strutturati

In uno scenario di BI, gli array di multiinsiemi possono essere utili per rappresentare e analizzare dati con più valori per un singolo attributo. Gli array di multiset possono essere particolarmente importanti in scenari in cui si hanno dati multivalore o gerarchici. Qui ci occupiamo di un caso d'uso comune, ovvero la gestione delle categorie di prodotti, dove un prodotto può appartenere a più categorie.

-- Create the Sales tableCREATE OR REPLACE TABLE Sales (Sale_ID INT PRIMARY KEY,Product_IDs VARIANT --INT);-- Inserting sample sales dataINSERT INTO Sales (Sale_ID, Product_IDs) SELECT 1, PARSE_JSON('[1, 3]');-- Products A and C in the same saleINSERT INTO Sales (Sale_ID, Product_IDs) SELECT 2, PARSE_JSON('[2, 4]');-- Products B and D in the same sale-- Create the Products tableCREATE OR REPLACE TABLE Products (Product_ID INT PRIMARY KEY,Product_Name VARCHAR,Product_Categories VARIANT --VARCHAR);-- Inserting sample data into ProductsINSERT INTO Products (Product_ID, Product_Name, Product_Categories) SELECT 1, 'Product A', ARRAY_CONSTRUCT('Electronics', 'Gadgets');INSERT INTO Products (Product_ID, Product_Name, Product_Categories) SELECT 2, 'Product B', ARRAY_CONSTRUCT('Clothing', 'Accessories');INSERT INTO Products (Product_ID, Product_Name, Product_Categories) SELECT 3, 'Product C', ARRAY_CONSTRUCT('Electronics', 'Appliances');INSERT INTO Products (Product_ID, Product_Name, Product_Categories) SELECT 4, 'Product D', ARRAY_CONSTRUCT('Clothing');

Utilizzando le tabelle appena create, come possiamo trovare categorie comuni tra i prodotti venduti insieme in una singola transazione come nell'esempio riportato di seguito?

Previous
Previous

Settimana 70 - Intermedio

Next
Next

Settimana 71 - Intermedio