0% found this document useful (0 votes)
123 views

First DWH - Script

This document contains SQL code to create the dimensional tables and time dimension for a data warehouse. It includes code to create tables for customers, products, stores, salespeople, and time. It defines primary keys and foreign keys. It also includes code for sequences and triggers to automatically generate surrogate keys for new records. Sample data is inserted into the dimension tables.

Uploaded by

Nelson Fleig
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
123 views

First DWH - Script

This document contains SQL code to create the dimensional tables and time dimension for a data warehouse. It includes code to create tables for customers, products, stores, salespeople, and time. It defines primary keys and foreign keys. It also includes code for sequences and triggers to automatically generate surrogate keys for new records. Sample data is inserted into the dimension tables.

Uploaded by

Nelson Fleig
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

First DWH: https://www.codeproject.

com/Articles/652108/Create-First-
Data-WareHouse
Oracle 11

CREATE TABLE DimCustomers (


CustomerID INT NOT NULL,
CustomerAltID VARCHAR(30) NOT NULL,
CustomerName VARCHAR(30),
Gender VARCHAR(30)
);

ALTER TABLE DimCustomers ADD (


CONSTRAINT customer_pk PRIMARY KEY (CustomerID)
);

CREATE SEQUENCE cust_seq START WITH 1;

CREATE OR REPLACE TRIGGER cust_bir


BEFORE INSERT ON DimCustomers
FOR EACH ROW

BEGIN
SELECT cust_seq.NEXTVAL
INTO :new.CustomerID
FROM dual;
END;

Create table DimProduct


(
ProductKey int primary key,
ProductAltKey varchar(10)not null,
ProductName varchar(100),
ProductActualCost NUMBER(19,4),
ProductSalesCost NUMBER(19,4)
)

CREATE SEQUENCE prod_seq START WITH 1;

CREATE OR REPLACE TRIGGER prod_bir


BEFORE INSERT ON DimProduct
FOR EACH ROW

BEGIN
SELECT prod_seq.NEXTVAL
INTO :new.ProductKey
FROM dual;
END;

INSERT ALL
INTO DimProduct(ProductAltKey,ProductName, ProductActualCost,
ProductSalesCost) VALUES ('ITM-001','Wheat Floor 1kg',5.50,6.50)
INTO DimProduct(ProductAltKey,ProductName, ProductActualCost,
ProductSalesCost) VALUES ('ITM-002','Rice Grains 1kg',22.50,24)
INTO DimProduct(ProductAltKey,ProductName, ProductActualCost,
ProductSalesCost) VALUES ('ITM-003','SunFlower Oil 1 ltr',42,43.5)
INTO DimProduct(ProductAltKey,ProductName, ProductActualCost,
ProductSalesCost) VALUES ('ITM-004','Nirma Soap',18,20)
INTO DimProduct(ProductAltKey,ProductName, ProductActualCost,
ProductSalesCost) VALUES ('ITM-005','Arial Washing Powder
1kg',135,139)

SELECT * FROM dual;


Create table DimStores
(
StoreID int primary key,
StoreAltID varchar(10)not null,
StoreName varchar(100),
StoreLocation varchar(100),
City varchar(100),
State varchar(100),
Country varchar(100)
);

CREATE SEQUENCE store_seq START WITH 1;

CREATE OR REPLACE TRIGGER store_bir


BEFORE INSERT ON DimStores
FOR EACH ROW

BEGIN
SELECT store_seq.NEXTVAL
INTO :new.StoreID
FROM dual;
END;

Insert all
into
DimStores(StoreAltID,StoreName,StoreLocation,City,State,Country )
values ('LOC-A1','X-Mart','S.P. RingRoad','Ahmedabad','Guj','India')
into
DimStores(StoreAltID,StoreName,StoreLocation,City,State,Country )
values ('LOC-A2','X-Mart','Maninagar','Ahmedabad','Guj','India')
into
DimStores(StoreAltID,StoreName,StoreLocation,City,State,Country )
values ('LOC-A3','X-Mart','Sivranjani','Ahmedabad','Guj','India')
SELECT * FROM dual;

Create table DimSalesPerson


(
SalesPersonID int primary key,
SalesPersonAltID varchar(10)not null,
SalesPersonName varchar(100),
StoreID int,
City varchar(100),
State varchar(100),
Country varchar(100)
);

CREATE SEQUENCE salesp_seq START WITH 1;

CREATE OR REPLACE TRIGGER salesp_bir


BEFORE INSERT ON DimSalesPerson
FOR EACH ROW

BEGIN
SELECT salesp_seq.NEXTVAL
INTO :new.SalesPersonID
FROM dual;
END;

Insert all
into
DimSalesPerson(SalesPersonAltID,SalesPersonName,StoreID,City,State,Cou
ntry ) values ('SP-DMSPR1','Ashish',1,'Ahmedabad','Guj','India')
into
DimSalesPerson(SalesPersonAltID,SalesPersonName,StoreID,City,State,Cou
ntry )values('SP-DMSPR2','Ketan',1,'Ahmedabad','Guj','India')
into
DimSalesPerson(SalesPersonAltID,SalesPersonName,StoreID,City,State,Cou
ntry )values('SP-DMNGR1','Srinivas',2,'Ahmedabad','Guj','India')
into
DimSalesPerson(SalesPersonAltID,SalesPersonName,StoreID,City,State,Cou
ntry )values('SP-DMNGR2','Saad',2,'Ahmedabad','Guj','India')
into
DimSalesPerson(SalesPersonAltID,SalesPersonName,StoreID,City,State,Cou
ntry )values('SP-DMSVR1','Jasmin',3,'Ahmedabad','Guj','India')
into
DimSalesPerson(SalesPersonAltID,SalesPersonName,StoreID,City,State,Cou
ntry )values('SP-DMSVR2','Jacob',3,'Ahmedabad','Guj','India')
SELECT * FROM dual;^

 Time Dimension
CREATE TABLE time_calendar_dim AS
SELECT CurrDate AS Day_ID,
1 AS Day_Time_Span,
CurrDate AS Day_End_Date,
TO_CHAR(CurrDate,'Day') AS Week_Day_Full,
TO_CHAR(CurrDate,'DY') AS Week_Day_Short,
TO_NUMBER(TRIM(leading '0' FROM TO_CHAR(CurrDate,'D'))) AS Day_Num_of_Week,
TO_NUMBER(TRIM(leading '0' FROM TO_CHAR(CurrDate,'DD'))) AS Day_Num_of_Month,
TO_NUMBER(TRIM(leading '0' FROM TO_CHAR(CurrDate,'DDD'))) AS Day_Num_of_Year,
UPPER(TO_CHAR(CurrDate,'Mon') || '-' || TO_CHAR(CurrDate,'YYYY')) AS Month_ID,
-- 31 AS Month_Time_Span,
MAX(TO_NUMBER(TO_CHAR(CurrDate, 'DD'))) OVER (PARTITION BY TO_CHAR(CurrDate,'Mon')) AS
Month_Time_Span,
--to_date('31-JAN-2010','DD-MON-YYYY') AS Month_End_Date,
MAX(CurrDate) OVER (PARTITION BY TO_CHAR(CurrDate,'Mon')) as Month_End_Date,
TO_CHAR(CurrDate,'Mon') || ' ' || TO_CHAR(CurrDate,'YYYY') AS Month_Short_Desc,
RTRIM(TO_CHAR(CurrDate,'Month')) || ' ' || TO_CHAR(CurrDate,'YYYY') AS Month_Long_Desc,
TO_CHAR(CurrDate,'Mon') AS Month_Short,
TO_CHAR(CurrDate,'Month') AS Month_Long,
TO_NUMBER(TRIM(leading '0'FROM TO_CHAR(CurrDate,'MM'))) AS Month_Num_of_Year,
'Q' || UPPER(TO_CHAR(CurrDate,'Q') || '-' || TO_CHAR(CurrDate,'YYYY')) AS Quarter_ID,
-- 31 AS Quarter_Time_Span,
COUNT(*) OVER (PARTITION BY TO_CHAR(CurrDate,'Q')) AS Quarter_Time_Span,
-- to_date('31-JAN-2010','DD-MON-YYYY') AS Quarter_End_Date,
MAX(CurrDate) OVER (PARTITION BY TO_CHAR(CurrDate,'Q')) AS Quarter_End_Date,
TO_NUMBER(TO_CHAR(CurrDate,'Q')) AS Quarter_Num_of_Year,
TO_CHAR(CurrDate,'YYYY') AS Year_ID,
--31 AS Year_Time_Span,
COUNT(*) OVER (PARTITION BY TO_CHAR(CurrDate,'YYYY')) AS Year_Time_Span,
-- to_date('31-JAN-2010','DD-MON-YYYY') AS Year_End_Date
MAX(CurrDate) OVER (PARTITION BY TO_CHAR(CurrDate,'YYYY')) Year_End_Date
FROM
(SELECT level n,
-- Calendar starts at the day after this date.
TO_DATE('31/12/2010','DD/MM/YYYY') + NUMTODSINTERVAL(level,'day') CurrDate
FROM dual
-- Change for the number of days to be added to the table.
CONNECT BY level <= 365)
ORDER BY CurrDate;

You might also like