Click here to Skip to main content
15,794,629 members
Articles / Programming Languages / SQL

Create First Data WareHouse

Rate me:
Please Sign up or sign in to vote.
4.93/5 (133 votes)
14 Sep 2013CPOL10 min read 550.5K   17.8K   136   79
In this article, I am going to show you the importance of data warehouse.


In this article, I am going to show you the importance of data warehouse? Why and when does an organization or company need to plan to go for data warehouse designing? We will take a quick look at the various concepts and then by taking one small scenario, we will design our First data warehouse and populate it with test data.

If you are thinking what is data warehouse, let me explain in brief, data warehouse is integrated, non volatile, subject oriented and time variant storage of data. Whenever your data is distributed across various databases, application or at various places stored in different formats and you want to convert this data into useful information by integrating and creating unique storage at a single location for these distributed data at that time, you need to start thinking to use data warehouse.

In another case, if your daily transactional data entry is very huge in your database, maybe millions or billions of records, then you need to archive these data to another Archive database which holds your historical data to remove load from live database and if you are creating your two dimensional report on this archive database then your report generation is very slow on that data it may take couple of minutes to couple of hours or it can give you timeout error. On this two dimensional data, even you cannot do any type of trend analysis on your data, you cannot divide your data into various time buckets of the day or cannot do study of data between various combination of year, quarter, month, week, day, weekday-weekend. In this scenario to take perfect decision on the basis of your historical data, you have to think to go for designing of data warehouse as per your requirement, so you can study data using multiple dimensions and can do better analysis to take accurate decision.

Designing of data warehouse helps to convert data into useful information, it provides multiple dimensions to study your data, so higher management can take Quick and accurate decision on the basis of statistics calculated using this data, this data can also be utilized for data mining, forecasting, predictive analysis, quicker reports, and Informative Dash board creation, which also helps management in day to day life to resolve various complex queries as per their requirement.

Now a day’s users need to have self service BI (Business Intelligence) capabilities so they can create reports on their own (Ad-Hoc reports) and can do analysis of data without much technical knowledge. Data warehousing is a business analyst's dream - all the information about the organization's activities gathered in one place, open to a single set of analytical tools. But how do you make the dream a reality? First, you have to plan your data warehouse system. So modeling of data warehouse is the first step in this direction.


X-Mart is having different malls in our city, where daily sales take place for various products. Higher management is facing an issue while decision making due to non availability of integrated data they can’t do study on their data as per their requirement. So they asked us to design a system which can help them quickly in decision making and provide Return on Investment (ROI).

Let us start designing of data warehouse, we need to follow a few steps before we start our data warehouse design.

Developing a Data Warehouse

The phases of a data warehouse project listed below are similar to those of most database projects, starting with identifying requirements and ending with executing the T-SQL Script to create data warehouse:

  1. Identify and collect requirements
  2. Design the dimensional model
  3. Execute T-SQL queries to create and populate your dimension and fact tables

Identify and Collect Requirements

We need to interview the key decision makers to know, what factors define the success in the business? How does management want to analyze their data? What are the most important business questions, which need to be satisfied by this new system?

We also need to work with persons in different departments to know the data and their common relations if any, document their entire requirement which need to be satisfied by this system.

Let us first identify the requirement from management about their requirements.

  1. Need to see daily, weekly, monthly, quarterly profit of each store.
  2. Comparison of sales and profit on various time periods.
  3. Comparison of sales in various time bands of the day.
  4. Need to know which product has more demand on which location?
  5. Need to study trend of sales by time period of the day over the week, month, and year?
  6. On what day sales is higher?
  7. On every Sunday of this month, what is sales and what is profit?
  8. What is trend of sales on weekday and weekend?
  9. Need to compare weekly, monthly and yearly sales to know growth and KPI?

Design the Dimensional Model

We need to design Dimensional Model to suit requirements of users which must address business needs and contains information which can be easily accessible. Design of model should be easily extensible according to future needs. This model design must supports OLAP cubes to provide "instantaneous" query results for analysts.

Let us take a quick look at a few new terms and then we will identify/derive it for our requirement.


The dimension is a master table composed of individual, non-overlapping data elements. The primary functions of dimensions are to provide filtering, grouping and labeling on your data. Dimension tables contain textual descriptions about the subjects of the business.

Let me give you a glimpse on different types of dimensions available like confirmed dimension, Role Playing dimension, Degenerated dimension, Junk Dimension.

Slowly changing dimension (SCD) specifies the way using which you are storing values of your dimension which is changing over a time and preserver the history. Different methods / types are available to store history of this change E.g. SCD1, SCD2, and SCD3 you can use as per your requirement.

Let us identify dimensions related to the above case study.

Product, Customer, Store, Date, Time, Sales person


A measure represents a column that contains quantifiable data, usually numeric, that can be aggregated. A measure is generally mapped to a column in a fact table. For your information, various types of measures are there. E.g. Additive, semi additive and Non additive.

Let us define what will be the Measures in our case.

Actual Cost, Total Sales, Quantity, Fact table record count

Fact Table

Data in fact table are called measures (or dependent attributes), Fact table provides statistics for sales broken down by customer, salesperson, product, period and store dimensions. Fact table usually contains historical transactional entries of your live system, it is mainly made up of Foreign key column which references to various dimension and numeric measure values on which aggregation will be performed. Fact tables are of different types, E.g. Transactional, Cumulative and Snapshot.

Let us identify what attributes should be there in our Fact Sales Table.

  1. Foreign Key Column

    Sales Date key, Sales Time key, Invoice Number, Sales Person ID, Store ID, Customer ID

  2. Measures

    Actual Cost, Total Sales, Quantity, Fact table record count

Design the Relational Database

We have done some basic workout to identify dimensions and measures, now we have to use appropriate schema to relate this dimension and Fact tables.

Few popular schemas used to develop dimensional model are as follows:

E.g. Star Schema, Snow Flake Schema, Star Flake Schema, Distributed Star Schema, etc.

In a different article, we will discuss all these schemas, dimension types, measure types, etc., in detail.

Personally, I will first try to use Star schema due to hierarchical attribute model it provides for analysis and speedy performance in querying the data.

Star schema the diagram resembles a star, with points radiating from a center. The center of the star consists of fact table and the points of the star are the dimension tables.

Let us create Our First Star Schema, please refer to the below figure:

Image 1

Using the Code

Let us execute our T-SQL Script step by step to create table and populate them with appropriate test values.

Follow the given steps to run the query in SSMS (SQL Server Management Studio).

  1. Open SQL Server Management Studio
  2. Connect Database Engine
  3. Open New Query editor
  4. Copy paste Scripts given below in various steps in new query editor window one by one
  5. To run the given SQL Script, press F5

Step 1

Create database for your Data Warehouse in SQL Server:

Createdatabase Sales_DW

Use Sales_DW

Step 2

Create Customer dimension table in Data Warehouse which will hold customer personal details.

Create table DimCustomer
CustomerID int primary key identity,
CustomerAltID varchar(10) not null,
CustomerName varchar(50),
Gender varchar(20)

Fill the Customer dimension with sample Values

Insert into DimCustomer(CustomerAltID,CustomerName,Gender)values
('IMI-001','Henry Ford','M'),
('IMI-002','Bill Gates','M'),
('IMI-003','Muskan Shaikh','F'),
('IMI-004','Richard Thrubin','M'),
('IMI-005','Emma Wattson','F');

Step 3

Create basic level of Product Dimension table without considering any Category or Subcategory

Create table DimProduct
ProductKey int primary key identity,
ProductAltKey varchar(10)not null,
ProductName varchar(100),
ProductActualCost money,
ProductSalesCost money


Fill the Product dimension with sample Values

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

Step 4

Create Store Dimension table which will hold details related stores available across various places.

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

Fill the Store Dimension with sample Values

Insert into DimStores(StoreAltID,StoreName,StoreLocation,City,State,Country )values
('LOC-A1','X-Mart','S.P. RingRoad','Ahmedabad','Guj','India'),

Step 5

Create Dimension Sales Person table which will hold details related stores available across various places.

Create table DimSalesPerson
SalesPersonID int primary key identity,
SalesPersonAltID varchar(10)not null,
SalesPersonName varchar(100),
StoreID int,
City varchar(100),
State varchar(100),
Country varchar(100)

Fill the Dimension Sales Person with sample values:

Insert into DimSalesPerson(SalesPersonAltID,SalesPersonName,StoreID,City,State,Country )values

Step 6

Create Date Dimension table which will create and populate date data divided on various levels.

For this, you have to refer my article on CodeProject Create and Populate Date Dimension.

Download the script and run it in this database for creating and filling of date dimension with values.

Step 7

Create Time Dimension table which will create and populate Time data for the entire day with various time buckets.

For this, you have to refer to my article on Code Project, Create & Populate Time Dimension with 24 Hour+ Values

Download the script and run it in this database for creating and filling of time dimension with values.

Step 8

Create Fact table to hold all your transactional entries of previous day sales with appropriate foreign key columns which refer to primary key column of your dimensions; you have to take care while populating your fact table to refer to primary key values of appropriate dimensions.


Customer Henry Ford has purchase purchased 2 items (sunflower oil 1 kg, and 2 Nirma soap) in a single invoice on date 1-jan-2013 from D-mart at Sivranjani and sales person was Jacob , billing time recorded is 13:00, so let us define how will we refer to the primary key values from each dimension.

Before filling fact table, you have to identify and do look up for primary key column values in dimensions as per given example and fill in foreign key columns of fact table with appropriate key values.

Attribute NameDimension TablePrimary Key Column/Value
Date (1-jan-2013), Sales Date Key (20130101) Dim DateDate Key: 20130101
Time (13:00:00) Sales Time Alt Key (130000)Dim Time Time Key: 46800
Composite key (Sales Person Alt ID+ Name ) for ('SP-DMSVR1'+’Jacob’)Dim Sales PersonSales Person ID: 6
Product Alt Key of (Sunflower Oil 1kg)'ITM-003'Dim ProductProduct ID: 3
Product Alt Key (Nirma Soap) 'ITM-004'Dim ProductProduct ID: 4
Store Alt ID of (Sivranjani store) 'LOC-A3'Dim StoreStore ID: 3
Customer Alt ID of (Henry Ford) is 'IMI-001'Dim CustomerCustomer ID: 1
Create Table FactProductSales
TransactionId bigint primary key identity,
SalesInvoiceNumber int not null,
SalesDateKey int,
SalesTimeKey int,
SalesTimeAltKey int,
StoreID int not null,
CustomerID int not null,
ProductID int not null,
SalesPersonID int not null,
Quantity float,
SalesTotalCost money,
ProductActualCost money,
Deviation float

Add Relation between Fact table and dimension tables:

-- Add relation between fact table foreign keys to Primary keys of Dimensions
FK_CustomerID FOREIGN KEY (CustomerID)REFERENCES Dimcustomer(CustomerID);
FK_ProductKey FOREIGN KEY (ProductID)REFERENCES Dimproduct(ProductKey);
FK_SalesPersonID FOREIGN KEY (SalesPersonID)REFERENCES Dimsalesperson(SalesPersonID);
FK_SalesDateKey FOREIGN KEY (SalesDateKey)REFERENCES DimDate(DateKey);
FK_SalesTimeKey FOREIGN KEY (SalesTimeKey)REFERENCES DimDate(TimeKey);

Populate your Fact table with historical transaction values of sales for previous day, with proper values of dimension key values.

Insert into FactProductSales(SalesInvoiceNumber,SalesDateKey,_
SalesTimeKey,SalesTimeAltKey,StoreID,CustomerID,ProductID ,_
StoreID,CustomerID,ProductID ,SalesPersonID,Quantity,_


StoreID,CustomerID,ProductID ,SalesPersonID,Quantity,ProductActualCost,SalesTotalCost,Deviation)




CustomerID,ProductID ,SalesPersonID,Quantity,ProductActualCost,SalesTotalCost,Deviation)




After executing the above T-SQL script, your sample data warehouse for sales will be ready, now you can create OLAP Cube on the basis of this data warehouse. I will shortly come up with the article to show how to create OLAP cube using this data warehouse.

In real life scenario, we need to design SSIS ETL package to populate dimension and fact table of data warehouse with appropriate values, we can schedule this package for daily execution and daily processing and populating of previous day data in dimension and fact tables, so our data will get ready for analysis and reporting.

Enjoy SQL Intelligence.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Architect Cybage Software Pvt. Ltd.
India India
Microsoft® Certified Professional (Microsoft Certification ID: 8918672).

Microsoft Certified Technology Specialist with more than 16+ years of expertise to architect and implement effective solutions for Data Analytics, Reporting and Data Visualization solutioning need on Azure Cloud or On-Premise

Technology :
Azure (Data Lake, Data Factory, Synapse Analytics, Databricks, SQL),
Microsoft BI (SSIS, SSAS, SSRS, SQL-Server), C#.Net, Pentaho,
Data Warehousing, Dimension modelling, Snowflake DW, SQL DW, MySQL
Data Visualization using (Tableau, Power BI, QlikView, Pentaho),
Domain : Sales, Retail, CRM, Public Transport, Media & Entertainment, Insurance
Data Integration and Analytics Experience with MS. Dynamic CRM, Salesforce CRM, Dataverse, SAP- FI, Dynamics AX etc.

Linked In Profile:
Click Here to View Linked In Profile

Change will not come if we keep waiting for some other person !!, or keep waiting for some other time !!, We are the one we are waiting for, We are the change that we are looking for.

Comments and Discussions

PraiseThanks Pin
Subhrajit Ghosh23-Feb-22 3:01
Subhrajit Ghosh23-Feb-22 3:01 
GeneralMy vote of 5 Pin
Subhrajit Ghosh23-Feb-22 2:58
Subhrajit Ghosh23-Feb-22 2:58 
QuestionReally gives a crystal clear understanding of dimension and fact table Pin
Vairavan Azagappan22-Jul-21 19:49
Vairavan Azagappan22-Jul-21 19:49 
QuestionInsert value in the fact tables Pin
Member 1509933713-Mar-21 10:41
Member 1509933713-Mar-21 10:41 
AnswerRe: Insert value in the fact tables Pin
Member 155279758-Feb-22 4:49
Member 155279758-Feb-22 4:49 
QuestionThanks. Pin
Member 1509933713-Mar-21 9:20
Member 1509933713-Mar-21 9:20 
QuestionSo Many Inexcusable errors Pin
Member 1486486119-Jun-20 5:52
Member 1486486119-Jun-20 5:52 
Questionhelp to solve this error Pin
StudingYet7-Apr-20 5:11
StudingYet7-Apr-20 5:11 
QuestionTheres no TimeKey Relation between Date and Time Pin
Member 1282297524-Dec-19 3:56
Member 1282297524-Dec-19 3:56 
Questionforeign key conflict Pin
Member 1465831017-Nov-19 0:54
Member 1465831017-Nov-19 0:54 
Questionvery helpful Pin
Member 1465801216-Nov-19 13:11
Member 1465801216-Nov-19 13:11 
Generallink to sales_dw Pin
Member 1419101625-Mar-19 7:24
Member 1419101625-Mar-19 7:24 
QuestionExcellent! Pin
rchacko20-Jun-18 23:52
rchacko20-Jun-18 23:52 
SuggestionYour long sentences Pin
r.kh4-Apr-18 9:21
r.kh4-Apr-18 9:21 
QuestionThe last foreign key constraint listed in the script is wrong Pin
Member 1096319820-Dec-17 0:59
Member 1096319820-Dec-17 0:59 
AnswerRe: The last foreign key constraint listed in the script is wrong Pin
Member 1509933713-Mar-21 9:18
Member 1509933713-Mar-21 9:18 
QuestionHi Pin
Mansour Elshaer19-Dec-17 4:20
Mansour Elshaer19-Dec-17 4:20 
SuggestionPopulating Fact table using SSIS Pin
MoniaBI11-Sep-17 6:31
MoniaBI11-Sep-17 6:31 
PraiseExcellent for beginners Thanks!! Pin
Member 1335419410-Aug-17 9:49
Member 1335419410-Aug-17 9:49 
QuestionDetailed and Informative Pin
EngrKI4-Jul-17 22:09
EngrKI4-Jul-17 22:09 
GeneralMy vote of 5 Pin
Alan Orellana17-Apr-17 13:06
Alan Orellana17-Apr-17 13:06 
GeneralMy vote of 5 Pin
Alan Orellana17-Apr-17 13:01
Alan Orellana17-Apr-17 13:01 
GeneralMy vote of 5 Pin
Manuel Cecconi4-Apr-17 23:56
Manuel Cecconi4-Apr-17 23:56 
QuestionQuery Pin
Member 130844923-Apr-17 0:50
Member 130844923-Apr-17 0:50 
QuestionVery helpful, my vote 5 Pin
Tony Dong15-Mar-17 12:49
Tony Dong15-Mar-17 12:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.