100% found this document useful (1 vote)
327 views4 pages

Data Staging Northwind

The document contains SQL code to create dimension tables for employees, shippers, customers, products, and time. It also creates a fact table to store sales data and populates the dimension tables with data from the Northwind database tables. Dimension tables are created with unique surrogate keys and populated by joining with corresponding tables from the source database. The fact table is populated by joining order details with orders and looking up dimension keys based on business keys.

Uploaded by

Subandi Wahyudi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
327 views4 pages

Data Staging Northwind

The document contains SQL code to create dimension tables for employees, shippers, customers, products, and time. It also creates a fact table to store sales data and populates the dimension tables with data from the Northwind database tables. Dimension tables are created with unique surrogate keys and populated by joining with corresponding tables from the source database. The fact table is populated by joining order details with orders and looking up dimension keys based on business keys.

Uploaded by

Subandi Wahyudi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

CREATE TABLE EMPLOYEE_DIM (

EMPLOYEE_KEY INTEGER IDENTITY(1,1) NOT NULL UNIQUE,


EMPLOYEE_ID INTEGER NOT NULL,
LASTNAME NVARCHAR(20) NOT NULL,
FIRSTNAME NVARCHAR(10) NOT NULL,
TITLE NVARCHAR(30) NOT NULL,
PRIMARY KEY (EMPLOYEE_KEY));

CREATE TABLE SHIPPER_DIM (


SHIPPER_KEY INTEGER IDENTITY(1,1) NOT NULL UNIQUE,
SHIPPER_ID INTEGER NOT NULL,
COMPANY_NAME NVARCHAR(40) NOT NULL,
PHONE NVARCHAR(24) NOT NULL,
PRIMARY KEY (SHIPPER_KEY));

CREATE TABLE CUSTOMER_DIM (


CUSTOMER_KEY INTEGER IDENTITY(1,1) NOT NULL UNIQUE,
CUSTOMER_ID NCHAR(5) NOT NULL,
COMPANY_NAME NVARCHAR(40) NOT NULL,
CONTACT_TITLE NVARCHAR(30) NOT NULL,
ADDRESS NVARCHAR(60) NOT NULL,
CITY NVARCHAR(15) NOT NULL,
REGION NVARCHAR(15),
POSTAL_CODE NVARCHAR(10),
COUNTRY NVARCHAR(15) NOT NULL,
PHONE NVARCHAR(24) NOT NULL,
FAX NVARCHAR(24),
PRIMARY KEY (CUSTOMER_KEY));

CREATE TABLE PRODUCT_DIM (


PRODUCT_KEY INTEGER IDENTITY(1,1) NOT NULL UNIQUE,
PRODUCT_ID INTEGER NOT NULL,
PRODUCT_NAME NVARCHAR(40) NOT NULL,
CATEGORY_ID INTEGER NOT NULL,
CATEGORY_NAME NVARCHAR(15) NOT NULL,
SUPPLIER_ID INTEGER NOT NULL,
DESCRIPTION NTEXT NOT NULL,
COMPANY_NAME NVARCHAR(40) NOT NULL,
CITY NVARCHAR(15) NOT NULL,
REGION NVARCHAR(15),
POSTAL_CODE NVARCHAR(10),
COUNTRY NVARCHAR(15) NOT NULL,
PRIMARY KEY (PRODUCT_KEY));

CREATE TABLE SALES_FACT (


PRODUCT_KEY INTEGER NOT NULL,
CUSTOMER_KEY INTEGER NOT NULL,
EMPLOYEE_KEY INTEGER NOT NULL,
TIME_KEY INTEGER NOT NULL,
SHIPPER_KEY INTEGER NOT NULL,
UNIT_PRICE MONEY NOT NULL,
QUANTITY SMALLINT NOT NULL,
DISCOUNT REAL NOT NULL,TOTAL REAL NOT NULL);
CREATE TABLE TIME_DIM (
TIME_KEY INTEGER IDENTITY(1,1) NOT NULL UNIQUE,
ORDER_DATE DATETIME NOT NULL,
DAY_VALUE NVARCHAR(10),
MONTH_VALUE NVARCHAR(10),
QUARTER_VALUE NVARCHAR(10),
YEAR_VALUE NVARCHAR(10),
PRIMARY KEY (TIME_KEY)
);

And this is the code to populate the dimension tables:

INSERT INTO EMPLOYEE_DIM(EMPLOYEE_ID,LASTNAME,FIRSTNAME,TITLE)


SELECT
NORTHWIND.DBO.EMPLOYEES.EMPLOYEEID,NORTHWIND.DBO.EMPLOYEES.LASTNA
ME, NORTHWIND.DBO.EMPLOYEES.FIRSTNAME,NORTHWIND.DBO.EMPLOYEES.TITLE
FROM NORTHWIND.DBO.EMPLOYEES

INSERT INTO SHIPPER_DIM(SHIPPER_ID,COMPANY_NAME,PHONE)


SELECT
NORTHWIND.DBO.SHIPPERS.SHIPPERID,NORTHWIND.DBO.SHIPPERS.COMPANYNAM
E,
NORTHWIND.DBO.SHIPPERS.PHONE FROM NORTHWIND.DBO.SHIPPERS

INSERT INTO
CUSTOMER_DIM(CUSTOMER_ID,COMPANY_NAME,CONTACT_TITLE,ADDRESS,CITY,R
EGION,POSTAL_CODE,COUNTRY,PHONE,FAX)
SELECT
NORTHWIND.DBO.CUSTOMERS.CUSTOMERID,NORTHWIND.DBO.CUSTOMERS.COMP
ANYNAME,NORTHWIND.DBO.CUSTOMERS.CONTACTTITLE,
NORTHWIND.DBO.CUSTOMERS.ADDRESS,NORTHWIND.DBO.CUSTOMERS.CITY,NOR
THWIND.DBO.CUSTOMERS.REGION,
NORTHWIND.DBO.CUSTOMERS.POSTALCODE,NORTHWIND.DBO.CUSTOMERS.COUN
TRY,NORTHWIND.DBO.CUSTOMERS.PHONE,
NORTHWIND.DBO.CUSTOMERS.FAX
FROM NORTHWIND.DBO.CUSTOMERS

INSERT INTO
PRODUCT_DIM(PRODUCT_ID,PRODUCT_NAME,CATEGORY_ID,CATEGORY_NAME,SUP
PLIER_ID,DESCRIPTION,COMPANY_NAME,CITY,REGION,POSTAL_CODE,COUNTRY)
SELECT
NORTHWIND.DBO.PRODUCTS.PRODUCTID,NORTHWIND.DBO.PRODUCTS.PRODUCTN
AME,NORTHWIND.DBO.CATEGORIES.CATEGORYID,
NORTHWIND.DBO.CATEGORIES.CATEGORYNAME,NORTHWIND.DBO.SUPPLIERS.SUP
PLIERID,NORTHWIND.DBO.CATEGORIES.DESCRIPTION,NORTHWIND.DBO.SUPPLIER
S.COMPANYNAME,
NORTHWIND.DBO.SUPPLIERS.CITY,NORTHWIND.DBO.SUPPLIERS.REGION,NORTHWI
ND.DBO.SUPPLIERS.POSTALCODE,NORTHWIND.DBO.SUPPLIERS.COUNTRY
FROM
NORTHWIND.DBO.PRODUCTS,NORTHWIND.DBO.CATEGORIES,NORTHWIND.DBO.SU
PPLIERS
WHERE NORTHWIND.DBO.CATEGORIES.CATEGORYID =
NORTHWIND.DBO.PRODUCTS.CATEGORYID
AND NORTHWIND.DBO.PRODUCTS.SUPPLIERID =
NORTHWIND.DBO.SUPPLIERS.SUPPLIERID

INSERT INTO TIME_DIM


(ORDER_DATE ,DAY_VALUE ,MONTH_VALUE,QUARTER_VALUE ,
YEAR_VALUE)
SELECT distinct ORDERDATE,DAY(ORDERDATE) as
tgl,MONTH(ORDERDATE) as
bulan,DATEPART(qq,ORDERDATE) as
quarter,YEAR(orderdate) as tahun
FROM NORTHWIND.DBO.ORDERS

Fact table
insert into sales_fact
(product_key,customer_key,employee_key,time_key,shipper_key,
unit_price,discount,quantity,Total)
select (select product_key from product_dim where
product_dim.PRODUCT_id=northwind.dbo.[order details].productid)
as product_key,
(select customer_key from customer_dim where
customer_dim.CUSTOMER_id=northwind.dbo.orders.customerid)
as customer_key,
(select employee_key from employee_dim where
employee_dim.EMPLOYEE_id=northwind.dbo.orders.employeeid)
as empployee_key,
(select time_key from time_dim where
time_dim.order_date=northwind.dbo.orders.orderdate) as TIME_KEY,
( select shipper_key from shipper_dim where
shipper_dim.shipper_id=northwind.dbo.orders.SHIPvia) as
shipper_key,
UNITPRICE, DISCOUNT, QUANTITY,((quantity*unitprice)*(1-discount))
as total
from northwind.dbo.[order details]
left join northwind.dbo.orders on northwind.dbo.[order
details].orderid=northwind.dbo.orders.orderid
order by northwind.dbo.[order details].orderid,northwind.dbo.
[order details].productid
FACT TABLE

SELECT
       COALESCE( Northwind_Mart.dbo.Time_Dim.TimeKey, 1 ),
       COALESCE( Northwind_Mart.dbo.Customer_Dim.CustomerKey, 1 ),
       COALESCE( Northwind_Mart.dbo.Product_Dim.ProductKey, 1 ),
       LineItemTotal = [Order Details].UnitPrice * [Order
Details].Quantity,
       LineItemQuantity = [Order Details].Quantity,
  FROM Orders
       INNER JOIN [Order Details] ON Orders.OrderID = [Order
Details].OrderID
       LEFT JOIN Northwind_Mart.dbo.Product_Dim ON [Order
Details].ProductID =
                  Northwind_Mart.dbo.Product_Dim.ProductID
       LEFT JOIN Northwind_Mart.dbo.Customer_Dim ON Orders.CustomerID =
                  Northwind_Mart.dbo.Customer_Dim.CustomerID
       LEFT JOIN Northwind_Mart.dbo.Time_Dim ON Orders.ShippedDate =
                  Northwind_Mart.dbo.Time_Dim.theDate
 WHERE (Orders.ShippedDate IS NOT NULL)

You might also like