0% found this document useful (0 votes)
20 views4 pages

7.11 Create Fact Tables & Load Data

Uploaded by

Drashti Turakhia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views4 pages

7.11 Create Fact Tables & Load Data

Uploaded by

Drashti Turakhia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

USE DATABASE "IMT577_DW_DRASHTI_TURAKHIA_STAGE";

--Creating and Inserting Fact_SalesActual

--DROP TABLE Fact_SalesActual;

CREATE OR REPLACE TABLE Fact_SalesActual(


DimProductID INTEGER CONSTRAINT FK_FactSalesActual_ProductID FOREIGN KEY
REFERENCES Dim_Product(DimProductID) Not Null
,DimStoreID INTEGER CONSTRAINT FK_FactSalesActual_StoreID FOREIGN KEY
REFERENCES Dim_Store(DimStoreID) Not Null
,DimResellerID INTEGER CONSTRAINT FK_FactSalesActual_ResellerID FOREIGN KEY
REFERENCES Dim_Reseller(DimResellerID) Not Null
,DimCustomerID INTEGER CONSTRAINT FK_FactSalesActual_CustomerID FOREIGN KEY
REFERENCES Dim_Customer(DimCustomerID) Not Null
,DimChannelID INTEGER CONSTRAINT FK_FactSalesActual_ChannelID FOREIGN KEY
REFERENCES Dim_Channel(DimChannelID) Not Null
,DimSaleDateID NUMBER (9) CONSTRAINT FK_FactSalesActual_DateID FOREIGN KEY
REFERENCES Dim_Date(Date_PKEY) Not Null
,DimLocationID INTEGER CONSTRAINT FK_FactSalesActual_LocationID FOREIGN KEY
REFERENCES Dim_Location(DimLocationID) Not Null
,SourceSalesHeaderID INTEGER
,SourceSalesDetailID INTEGER
,SalesAmount NUMBER (38,2)
,SalesQuantity NUMBER (38,0)
,SalesUnitPrice NUMBER (38,2)
,SalesExtendedCost NUMBER (38,2)
,SalesTotalProfit NUMBER (38,2)
);

INSERT INTO Fact_SalesActual (


DimProductID
,DimStoreID
,DimResellerID
,DimCustomerID
,DimChannelID
,DimSaleDateID
,DimLocationID
,SourceSalesHeaderID
,SourceSalesDetailID
,SalesAmount
,SalesQuantity
,SalesUnitPrice
,SalesExtendedCost
,SalesTotalProfit
)
SELECT
NVL(dp.DimProductID, -1),
NVL(ds.DimStoreID, -1),
NVL(dr.DimResellerID,-1),
NVL(dc.DimCustomerID,-1),
NVL(dch.DimChannelID,-1),
// NVL(dd.DATE_PKEY,-1),
CAST(REPLACE(REPLACE(CAST(ssh.date AS DATE), '00', '20'), '-', '') AS NUMBER(9)) AS
DATE_PKEY,
NVL(dl.DimLocationID,-1),
NVL(ssh.SALESHEADER,-1),
NVL(ssd.SalesDetailID,-1),
ssd.SalesAmount,
ssd.SalesQuantity,
(ssd.SalesAmount/ssd.SalesQuantity) AS "SalesUnitPrice",
(dp.ProductCost*ssd.SalesQuantity) AS "SalesExtendedCost",
(ssd.SalesAmount- (dp.ProductCost*ssd.SalesQuantity)) AS "SalesTotalProfit"
FROM
STAGE_SALESDETAIL ssd
join STAGE_SALESHEADER ssh on ssd.SALESHEADERID = ssh.SALESHEADER
left outer join DIM_Product dp on ssd.ProductID= dp.ProductID
left outer join DIM_Store ds on ssh.storeID=ds.SourceStoreID
left outer join DIM_Reseller dr on ssh.ResellerID= dr.ResellerID
left outer join DIM_Customer dc on ssh.CustomerID=dc.CustomerID
left outer join DIM_Channel dch on ssh.ChannelID= dch.ChannelID
left outer join DIM_Date dd on ssh.Date= dd.Date
left outer join DIM_Location dl on dl.DimLocationID = ds.DimLocationID;

select * from Fact_SalesActual;

--Creating and Inserting Fact_ProductSalesTarget

CREATE OR REPLACE TABLE Fact_ProductSalesTarget(


DimProductID INTEGER CONSTRAINT FK_FactProductSalesTarget_ProductID FOREIGN
KEY REFERENCES Dim_Product(DimProductID) Not Null
,DimTargetDateID NUMBER(9) CONSTRAINT FK_FactProductSalesTarget_DateID
FOREIGN KEY REFERENCES Dim_Date(Date_PKEY) Not Null
,ProductTargetSalesQuantity NUMBER(38,0)
);

--DROP TABLE Fact_ProductSalesTarget;


Insert into Fact_ProductSalesTarget
(
DimProductID,
DimTargetDateID,
ProductTargetSalesQuantity
)
Select
dp.DimProductID,
dd.DATE_PKEY,
stdp.SALESQUANTITYTARGET/365 AS "ProductTargetSalesQuantity"
From
STAGE_TARGETDATAPRODUCT stdp left join Dim_Product dp on stdp.ProductID=
dp.ProductID
left outer join DIM_Date dd on stdp.Year= dd.Year;

select * from Fact_ProductSalesTarget;

--Creating and Inserting Fact_SRCSalesTarget

CREATE OR REPLACE TABLE Fact_SRCSalesTarget(


DimStoreID INTEGER CONSTRAINT FK_FactSRCSalesTarget_StoreID FOREIGN KEY
REFERENCES Dim_Store(DimStoreID) Not Null
,DimResellerID INTEGER CONSTRAINT FK_FactSRCSalesTarget_ResellerID FOREIGN
KEY REFERENCES Dim_Reseller(DimResellerID) Not Null
,DimChannelID INTEGER CONSTRAINT FK_FactSRCSalesTarget_ChannelID FOREIGN
KEY REFERENCES Dim_Channel(DimChannelID) Not Null
,DimTargetDateID NUMBER(9) CONSTRAINT FK_FactSRCSalesTarget_DateID FOREIGN
KEY REFERENCES Dim_Date(Date_PKEY) Not Null
,SalesTargetAmount NUMERIC(38,0)
);

--DROP TABLE Fact_ProductSalesTarget;

INSERT INTO FACT_SRCSALESTARGET


(
dimStoreID
,dimResellerID
,dimChannelID
,DIMTARGETDATEID
,SALESTARGETAMOUNT
)
SELECT
NVL(dim_store.dimstoreID, -1) AS dimstoreid,
NVL(dim_reseller.dimresellerID, -1) AS dimresellerid,
NVL(dim_channel.dimchannelID, -1) AS dimchannelID,
dim_date.date_pkey AS dimtargetdateID,
stage_CRS.targetsalesamount AS salestargetamount
FROM STAGE_TARGETDATACHANNELRESELLERSTORE stage_CRS
LEFT OUTER JOIN dim_reseller ON dim_reseller.resellername = stage_CRS.targetname
INNER JOIN dim_date ON dim_date.year = stage_CRS.year
LEFT JOIN dim_channel ON dim_channel.channelname = stage_CRS.CHANNELNAME
LEFT JOIN Dim_Store ON Dim_Store.storenumber = CASE
WHEN stage_CRS.TARGETNAME = 'Store Number 5' then 5
WHEN stage_CRS.TARGETNAME = 'Store Number 8' then 8
WHEN stage_CRS.TARGETNAME = 'Store Number 10' then 10
WHEN stage_CRS.TARGETNAME = 'Store Number 21' then 21
WHEN stage_CRS.TARGETNAME = 'Store Number 34' then 34
WHEN stage_CRS.TARGETNAME = 'Store Number 39' then 39
END;

Select * from Fact_SRCSalesTarget;

You might also like