Escolar Documentos
Profissional Documentos
Cultura Documentos
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
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)