







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!








/* dw_regular.sql / / / /*****************************************************************/ USE dw; / customer dimension loading / 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) ; / SCD2 on customer street addresses / / first, expire the existing customers / UPDATE customer_dim a , customer_stg b SET expiry_date = SUBDATE (CURRENT_DATE, 1) WHERE a.customer_number = b.customer_number AND a.customer_street_address <> b.customer_street_address AND expiry_date = '9999โ 12 โ 31' ; / then, add a new row for the customer */ INSERT INTO customer_dim SELECT
/* product dimension loading / TRUNCATE product_stg ; LOAD DATA INFILE 'product.txt' INTO TABLE product_stg FIELDS TERMINATED BY ' ' OPTIONALLY ENCLOSED BY ' ' LINES TERMINATED BY '\r\n' IGNORE 1 LINES ( product_code , product_name , product_category) ; / SCD2 on product name and group / / first, expire the existing product / UPDATE product_dim a , product_stg b SET expiry_date = SUBDATE (CURRENT_DATE, 1) WHERE a.product_code = b.product_code AND ( a.product_name <> b.product_name OR a.product_category <> b.product_category) AND expiry_date = '9999โ 12 โ 31' ; / then, add a new row for the product */ 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') ; /* 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 loading */ INSERT INTO order_dim ( order_sk , order_number , effective_date , expiry_date) SELECT NULL , order_number , order_date , '9999โ 12 โ 31' FROM source.sales_order WHERE entry_date = CURRENT_DATE ; INSERT INTO sales_order_fact SELECT order_sk , customer_sk , product_sk , date_sk , order_amount 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
/* sales_order_regular.sql / / / /*****************************************************************/ USE source; INSERT INTO sales_order VALUES (22, 1, 1, '2007โ 03 โ 01', '2007โ 03 โ 01', 1000) , (23, 2, 2, '2007โ 03 โ 01', '2007โ 03 โ 01', 2000) , (24, 3, 3, '2007โ 03 โ 01', '2007โ 03 โ 01', 3000) , (25, 4, 4, '2007โ 03 โ 01', '2007โ 03 โ 01', 4000) , (26, 5, 2, '2007โ 03 โ 01', '2007โ 03 โ 01', 1000) , (27, 6, 2, '2007โ 03 โ 01', '2007โ 03 โ 01', 3000) , (28, 7, 3, '2007โ 03 โ 01', '2007โ 03 โ 01', 5000) , (29, 8, 4, '2007โ 03 โ 01', '2007โ 03 โ 01', 7000) , (30, 1, 1, '2007โ 03 โ 01', '2007โ 03 โ 01', 1000) , (31, 2, 2, '2007โ 03 โ 01', '2007โ 03 โ 01', 2000) , (32, 3, 3, '2007โ 03 โ 01', '2007โ 03 โ 01', 4000) , (33, 4, 4, '2007โ 03 โ 01', '2007โ 03 โ 01', 6000) , (34, 5, 1, '2007โ 03 โ 01', '2007โ 03 โ 01', 2500) , (35, 6, 2, '2007โ 03 โ 01', '2007โ 03 โ 01', 5000) , (36, 7, 3, '2007โ 03 โ 01', '2007โ 03 โ 01', 7500) , (37, 8, 4, '2007โ 03 โ 01', '2007โ 03 โ 01', 1000) ; / end of script */
mysql> . c:\mysql\scripts\sales_order_regular.sql
Database changed Query OK, 16 rows affected (0.08 sec) Records: 16 Duplicates: 0 Warnings: 0
mysql> . c:\mysql\scripts\dw_regular.sql
Database changed Query OK, 7 rows affected (0.12 sec) Query OK, 8 rows affected (0.05 sec) Records: 8 Deleted: 0 Skipped: 0 Warnings: 0 Query OK, 1 row affected (0.06 sec) Rows matched: 1 Changed: 1 Warnings: 0 Query OK, 1 row affected (0.07 sec) Records: 1 Duplicates: 0 Warnings: 0 Query OK, 2 rows affected (0.06 sec) Rows matched: 2 Changed: 2 Warnings: 0 Query OK, 1 row affected (0.06 sec) Records: 1 Duplicates: 0 Warnings: 0 Query OK, 3 rows affected (0.05 sec) Query OK, 4 rows affected (0.08 sec) Records: 4 Deleted: 0 Skipped: 0 Warnings: 0 Query OK, 1 row affected (0.05 sec) Rows matched: 1 Changed: 1 Warnings: 0 Query OK, 1 row affected (0.06 sec) Records: 1 Duplicates: 0 Warnings: 0 Query OK, 1 row affected (0.06 sec) Records: 1 Duplicates: 0 Warnings: 0 Query OK, 16 rows affected (0.07 sec) Records: 16 Duplicates: 0 Warnings: 0 Query OK, 16 rows affected (0.11 sec) Records: 16 Duplicates: 0 Warnings: 0
customer_number: 6 customer_name: Extremely Loyal Clients customer_street_address: 7070 Ritter Rd. customer_zip_code: 17055 customer_city: Pittsburgh customer_state: PA 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 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 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 effective_date: 2007 โ 03 โ 01 expiry_date: 9999 โ 12 โ 31 9 rows in set (0.00 sec)
mysql> select * from product_dim \G
*************************** 1. row ***************************
product_sk: 1 product_code: 1 product_name: Hard Disk Drive product_category: Storage effective_date: 2005 โ 03 โ 01 expiry_date: 9999 โ 12 โ 31 *************************** 2. row *************************** product_sk: 2 product_code: 2 product_name: Floppy Drive product_category: Storage effective_date: 2005 โ 03 โ 01 expiry_date: 9999 โ 12 โ 31 *************************** 3. row *************************** product_sk: 3 product_code: 3 product_name: LCD Panel product_category: Monitor effective_date: 2005 โ 03 โ 01 expiry_date: 2007 โ 02 โ 28 *************************** 4. row *************************** product_sk: 4 product_code: 3 product_name: Flat Panel product_category: Monitor effective date: 2007 โ 03 โ 01 expiry_date: 9999 โ 12 โ 31 *************************** 5. row ************************** product_sk: 5 product_code: 4 product_name: Keyboard product_category: Peripheral effective_date: 2007 โ 03 โ 01 expiry_date: 9999 โ 12 โ 31 5 rows in set (0.01 sec)
mysql> select * from order_dim;
| order_sk | order_number | effective_date | expiry_date | +----------+--------------+----------------+-------------+ | 1 | 3 | 2005 โ 03 โ 01 | 9999 โ 12 โ 31 | | 2 | 4 | 2005 โ 04 โ 15 | 9999 โ 12 โ 31 | | 3 | 5 | 2005 โ 05 โ 20 | 9999 โ 12 โ 31 |
35 rows ir n set (0.00 sec :)
Summary