











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!












/* shipping_address.sql / / */ /*****************************************************************/ USE dw; ALTER TABLE customer_dim ADD shipping_address CHAR (50) AFTER customer_state , ADD shipping_zip_code INT (5) AFTER shipping_address , ADD shipping_city CHAR (30) AFTER shipping_zip_code , ADD shipping_state CHAR (2) AFTER shipping_city ;
ALTER TABLE customer_stg ADD shipping_address CHAR (50) AFTER customer_state , ADD shipping_zip_code INT (5) AFTER shipping_address , ADD shipping_city CHAR (30) AFTER shipping_zip_code , ADD shipping_state CHAR (2) AFTER shipping_city ; /* end of script */
mysql> . c:\mysql\scripts\shipping_address.sql
Database changed Query OK, 9 rows affected (0.73 sec) Records: 9 Duplicates: 0 Warnings: 0 Query OK, 8 rows affected (0.49 sec) Records: 8 Duplicates: 0 Warnings: 0
mysql> select * from customer_dim \G
*************************** 1. row *************************** customer_sk: 1 customer_number: 1 customer_name: Really Large Customers customer_street_address: 7500 Louise Dr. customer_zip_code: 17050 customer_city: Mechanicsburg customer_state: PA shipping_address: NULL shipping_zip_code: NULL shipping_city: NULL shipping_state: NULL effective_date: 2005 – 03 – 01 expiry_date: 9999 – 12 – 31 *************************** 2. row *************************** customer_sk: 2 customer_number: 2 customer_name: Small Stores customer_street_address: 2500 Woodland St. customer_zip_code: 17055 customer_city: Pittsburgh customer_state: PA shipping_address: NULL shipping_zip_code: NULL shipping_city: NULL
effective date: 2005 – 03 – 01 expiry_date: 2007 – 02 – 28 *************************** 7. row *************************** customer_sk: 7 customer_number: 7 customer_name: Distinguished Agencies customer_street_address: 9999 Scott St. customer_zip_code: 17050 customer_city: Mechanicsburg customer_state: PA shipping_address: NULL shipping_zip_code: NULL shipping_city: NULL shipping_state: NULL effective date: 2005 – 03 – 01 expiry_date: 9999 – 12 – 31 *************************** 8. row *************************** customer_sk: 8 customer_number: 6 customer_name: Extremely Loyal Clients customer_street_address: 7777 Ritter Rd. customer_zip_code: 17055 customer_city: Pittsburgh customer_state: PA shipping_address: NULL shipping_zip_code: NULL shipping_city: NULL shipping_state: NULL effective_date: 2007 – 03 – 01 expiry_date: 9999 – 12 – 31 *************************** 9. row *************************** customer_sk: 9 customer_number: 8 customer_name: Subsidiaries customer_street_address: 10000 Wetline Blvd. customer_zip_code: 17055 customer_city: Pittsburgh customer_state: PA shipping_address: NULL shipping_zip_code: NULL shipping_city: NULL shipping_state: NULL effective_date: 2007 – 03 – 01 expiry_date: 9999 – 12 – 31 9 rows in set (0.00 sec)
/* order_quantity.sql / / */
USE dw; ALTER TABLE sales_order_fact ADD order_quantity INT AFTER order_amount ; /* end of script */
mysql> . c:\mysql\scripts\order_quantity.sql
Database changed Query OK, 35 rows affected (0.56 sec) Records: 35 Duplicates: 0 Warnings: 0
mysql> select order_sk osk, customer_sk csk, product_sk psk, order_date_sk odsk, -> order_amount amt, order_quantity qty -> from sales_order_fact;
/* dw_regular_10.sql / / / /*****************************************************************/ USE dw; / CUSTOMER_DIM POPULATION / TRUNCATE customer_stg; LOAD DATA INFILE 'customer.csv' INTO TABLE customer_stg FIELDS TERMINATED BY ' , ' OPTIONALLY ENCLOSED BY ' " ' LINES TERMINATED BY '\r\n' IGNORE 1 LINES ( customer_number , customer_name , customer_street_address , customer_zip_code , customer_city , customer_state , shipping_address , shipping_zip_code , shipping_city , shipping_state ) ; / SCD 2 ON ADDRESSES */ UPDATE customer_dim a , customer_stg b SET a.expiry_date = SUBDATE (CURRENT_DATE, 1) WHERE a.customer_number = b.customer_number AND ( a.customer_street_address <> b.customer_street_address OR a.customer_city <> b.customer_city OR a.customer_zip_code <> b.customer_zip_code OR a.customer_state <> b.customer_state OR a.shipping_address <> b.shipping_address OR a.shipping_city <> b.shipping_city OR a.shipping_zip_code <> b.shipping_zip_code OR a.shipping_state <> b.shipping_state OR a.shipping_address IS NULL OR a.shipping_city IS NULL OR a.shipping_zip_code IS NULL OR a.shipping_state IS NULL) AND expiry_date = '9999-12-31'
INSERT INTO customer_dim SELECT NULL , b.customer_number , b.customer_name , b.customer_street_address , b.customer_zip_code , b.customer_city , b.customer_state , b.shipping_address , b.shipping_zip_code , b.shipping_city , b.shipping_state , CURRENT_DATE , '9999-12-31' FROM customer_dim a , customer_stg b WHERE a.customer_number = b.customer_number AND ( a.customer_street_address <> b.customer_street_address OR a.customer_city <> b.customer_city OR a.customer_zip_code <> b.customer_zip_code OR a.customer_state <> b.customer_state OR a.shipping_address <> b.shipping_address OR a.shipping_city <> b.shipping_city OR a.shipping_zip_code <> b.shipping_zip_code OR a.shipping_state <> b.shipping_state OR a.shipping_address IS NULL OR a.shipping_city IS NULL OR a.shipping_zip_code IS NULL OR a.shipping_state IS NULL) AND EXISTS ( SELECT * FROM customer_dim x WHERE b.customer_number = x.customer_number AND a.expiry_date = SUBDATE (CURRENT_DATE, 1)) AND NOT EXISTS ( SELECT * FROM customer_dim y WHERE b.customer_number = y.customer_number AND y.expiry_date = '9999– 12 – 31') ; /* END OF SCD 2 / / SCD 1 ON NAME */ UPDATE customer_dim a, customer_stg b SET a.customer_name = b.customer_name WHERE a.customer_number = b.customer_number AND a.expiry_date = '9999– 12 – 31' AND a.customer_name <> b.customer_name ;
INSERT INTO product_dim SELECT NULL , b.product_code , b.product_name , b.product_category , CURRENT_DATE , '9999– 12 – 31' FROM product_dim a , product_stg b WHERE a.product_code = b.product_code AND ( a.product_name <> b.product_name OR a.product_category <> b.product_category ) AND EXISTS ( SELECT * FROM product_dim x WHERE b.product_code = x.product_code AND a.expiry_date = SUBDATE (CURRENT_DATE, 1) AND NOT EXISTS ( SELECT * FROM product_dim y WHERE b.product_code = y.product_code AND y.expiry_date = '9999– 12 – 31') ; /* END OF SCD 2 / / ADD NEW PRODUCT / INSERT INTO product_dim SELECT NULL , product_code , product_name , product_category , CURRENT_DATE , '9999– 12 – 31' FROM product_stg WHERE product_code NOT IN( SELECT y.product_code FROM product_dim x, product_stg y WHERE x.product_code = y.product_code ) ; / END OF PRODUCT_DIM POPULATION / / ORDER_DIM POPULATION */ INSERT INTO order_dim ( order_sk , order_number , effective_date , expiry_date
, order_number , order_date , '9999– 12 – 31' FROM source.sales_order WHERE entry_date = CURRENT_DATE ; /* END OF ORDER_DIM POPULATION / / SALES_ORDER_FACT POPULATION / INSERT INTO sales_order_fact SELECT order_sk , customer_sk , product_sk , date_sk , order_amount , order_quantity FROM source.sales order a , order_dim b , customer_dim c , product_dim d , date_dim e WHERE a.order_number = b.order_number AND a.customer_number = c.customer_number AND a.order_date >= c.effective_date AND a.order_date <= c.expiry_date AND a.product_code = d.product_code AND a.order_date >= d.effective_date AND a.order_date <= d.expiry_date AND a.order_date = e.date AND a.entry_date = CURRENT_DATE ; / end of script */
Testing
Database changed Query OK, 37 rows affected (0.39 sec) Records: 37 Duplicates: 0 Warnings: 0
/* sales_order_quantity_data.sql / / / /*****************************************************************/ USE source; INSERT INTO sales_order VALUES (38, 1, 1, '2007-03-02', '2007-03-02', 1000, 10) , (39, 2, 2, '2007-03-02', '2007-03-02', 2000, 20) , (40, 3, 3, '2007-03-02', '2007-03-02', 4000, 40) , (41, 4, 4, '2007-03-02', '2007-03-02', 6000, 60) , (42, 5, 1, '2007-03-02', '2007-03-02', 2500, 25) , (43, 6, 2, '2007-03-02', '2007-03-02', 5000, 50) , (44, 7, 3, '2007-03-02', '2007-03-02', 7500, 75) , (45, 8, 4, '2007-03-02', '2007-03-02', 1000, 10) , (46, 9, 1, '2007-03-02', '2007-03-02', 1000, 10) ; / end of script */
mysql> . c:\mysql\scripts\dw_regular_10.sql
Database changed Query OK, 8 rows affected (0.05 sec) Query OK, 9 rows affected (0.06 sec) Records: 9 Deleted: 0 Skipped: 0 Warnings: 0 Query OK, 8 rows affected (0.06 sec) Rows matched: 8 Changed: 8 Warnings: 0 Query OK, 8 rows affected (0.05 sec) Records: 8 Duplicates: 0 Warnings: 0
Query OK, 0 rows affected (0.01 sec) Rows matched: 0 Changed: 0 Warnings: 0 Query OK, 1 row affected (0.07 sec) Records: 1 Duplicates: 0 Warnings: 0 Query OK, 4 rows affected (0.05 sec) Query OK, 4 rows affected (0.06 sec) Records: 4 Deleted: 0 Skipped: 0 Warnings: 0 Query OK, 0 rows affected (0.00 sec) Rows matched: 0 Changed: 0 Warnings: 0 Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0 Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0 Query OK, 9 rows affected (0.07 sec) Records: 9 Duplicates: 0 Warnings: 0 Query OK, 9 rows affected (0.10 sec) Records: 9 Duplicates: 0 Warnings: 0
mysql> select customer_number no, customer_name name, -> shipping_city, shipping_zip_code zip, shipping_state st, -> effective_date eff, expiry_date exp -> from customer_dim \G
******************************1.row************************ no: 1 name: Really Large Customers shipping_city: NULL zip: NULL st: NULL eff: 2005-03- exp: 2007-03- ******************************2. row ************************* no: 2 name: Small Stores shipping_city: NULL zip: NULL st: NULL eff: 2005-03- exp: 2007-03- ******************************3. row ************************* no: 3
shipping_city: Mechanicsburg zip: 17050 st: PA eff: 2007-03- exp: 9999-12- *************************** 11. row *************************** no: 2 name: Small Stores shipping_city: Pittsburgh zip: 17055 st: PA eff: 2007-03- exp: 9999-12- *************************** 12. row *************************** no: 3 name: Medium Retailers shipping_city: Pittsburgh zip: 17055 st: PA eff: 2007-03- exp: 9999-12- *************************** 13. row *************************** no: 4 name: Good Companies shipping_city: Mechanicsburg zip: 17050 st: PA eff: 2007-03- exp: 9999-12- *************************** 14. 4 row *************************** no: 5 name: Wonderful Shops shipping_city: Mechanicsburg zip: 17050 st: PA eff: 2007-03- exp: 9999-12- *************************** 15. 5 row *************************** no: 6 name: Extremely Loyal Clients shipping_city: Pittsburgh zip: 17055 st: PA eff: 2007-03- exp: 9999-12- *************************** 16. row *************************** no: 7 name: Distinguished Agencies shipping_city: Mechanicsburg zip: 17050 st: PA eff: 2007-03- exp: 9999-12- *************************** 17. row *************************** no: 8 name: Subsidiaries shipping_city: Pittsburgh
zip: 17055 st: PA eff: 2007-03- exp: 9999-12- *************************** 18. row *************************** no: 9 name: Online Distributors shipping_city: Pittsburgh zip: 17055 st: PA eff: 2007-03- exp: 9999-12- 18 rows in set (0.00 sec)
mysql> select order_sk o_sk, customer_sk c_sk, product_sk p_sk, order_date_sk od_sk, -> order_amount amt, order_quantity qty -> from sales_order_fact;
Summary