As the name says, Business Rules are rules that apply to a company. Anyone with common sense can come up with Business Rules. In Data Warehousing you also deal with Business Rules and with how you handle them.
Do you throw away records that do not meet the Business Rules, put them aside in a reject table, or just load them? These are choices you have to make when you develop a Data Warehouse.
Examples of Business Rules
- The delivery date of an order may not be before the order date;
- An order must be shipped within 3 days;
- A sofi number (Dutch social security number) must have 9 digits;
- The contract hours of an employee may not be more than 40 hours;
- ETC.
Mapping the Business Rules
As you can imagine, the number of Business Rules can be huge. It is important to map out which Business Rules really matter. There is no point, for example, in measuring how many people named Piet are female. What you do want to know is how many employees in the system have a contract of more than 40 hours. Once you have made a list of Business Rules and the criteria the Business Rules must meet, the question is: what do you want to do with it? One goal can be to map how many records stored in the Data Warehouse do not meet the Business Rules, to judge the quality of the data. Another goal can be to improve the quality of the data.
Loading the Data Warehouse
Every day, new data from the source systems is added to the data in the Data Warehouse. Every day staff will join (in large organisations) and leave. As soon as the load process (ETL) is done, we want to know how many records in the Data Warehouse do not meet the Business Rules. We then want to store this for every day. Because we store this every day, we can create a trend line. On 1-1-2008 the number of employees with a 40+ contract was 1168, for example, and on 1 July 2008 it was only 500. Because we store this per day, we can see whether the quality of the data has improved over time.
Business Rule: An employee may not have more than a 40-hour contract
Date Count
20080101 1168
20080102 1160
…
20080701 500
…
This article describes a method to build a Business Rule Validator, so you can manage the data in the Data Warehouse with Business Rules. It describes how to count the numbers and how to store them. After that you can download two reports.
Step 1 - Create the tables
The first step is to create a table in which we put the Business Rules.
CREATE TABLE [dbo].[BusinessRuleViews]([BusinessRuleViewID] [int] IDENTITY(1,1) NOT NULL,[BusinessRuleViewName] [varchar](256) NULL,[BusinessRuleViewDesc] [varchar](1000) NULL,[BusinessRuleViewTableName] [varchar](1000) NULL) ON [PRIMARY]The second table we need is a table in which we store the number of records that do not meet the Business Rule.
CREATE TABLE [dbo].[BusinessRuleViewsCount]([BusinessRuleViewCountID] [int] IDENTITY(1,1) NOT NULL,[BusinessRuleViewID] [int] NULL,[BusinessRuleViewCountRunno] [int] NULL,[BusinessRuleViewCountDate] [datetime] NULL,[BusinessRuleViewCountCount] [int] NULL,[BusinessRuleViewCountTotTableCount] [int] NULL,[BusinessRuleViewCountDesc] [varchar](100) NULL) ON [PRIMARY]Step 2 - Create a View per Business Rule
For every Business Rule we want to measure, we make a Query that counts the rows that do not meet the Business Rule. We store this Query in a View.
CREATE VIEW [dbo].[vwBR_DimMedewerker_MeerDan40UrigContract]AS
SELECT SofiNummerFROM DimWerknemerWHERE ContractUren > 40Keep the naming consistent, or all Views will end up mixed together. Best Practice is to start with “vwBR” (View Business Rule), followed by the name of the table you want to measure and then the name of the Business Rule. This way you create a View for every Business Rule that gets the rows that do not meet the Business Rule.
Step 3 - Fill the table
Next we fill the table we created in step 1. We have to store which Views we made, so we can read them later.
| Column name | Content |
| [BusinessRuleViewID] | Identity field (filled automatically) |
| [BusinessRuleViewName] | vwBR_DimMedewerker_MeerDan40UrigContract |
| [BusinessRuleViewDesc] | Number of employees who have more than 40 hours on their contract |
| [BusinessRuleViewTableName] | DimWerknemer |
This way you fill in one record for every Business Rule.
Step 4 - Load the counts per day
The next step is to run a Stored Procedure that reads all the counts in the Views and then writes them to the table we made in step 1. You can create the Stored Procedure below. Make sure you change the database names to the database in which you created the tables / views!
CREATE PROCEDURE [dbo].[SP_Execute_BusinessRules_Views]ASBEGINDECLARE @vViewName varchar(100)DECLARE @SQLString nvarchar(500)DECLARE @ParmDefinition nvarchar(500)DECLARE @iCount_rec varchar(30)DECLARE @iRunNo IntDECLARE @vcTableName nVarchar(100)DECLARE @iCount_TotRec intDECLARE @icount_TotRecOUT intDECLARE @iTotRowCount int
SET @iRunNo = (SELECT Max(BusinessRuleViewCountRunno)FROM DATABASENAAM..BusinessRuleViewsCount)
IF @iRunNo IS NULLSET @iRunno = 1ELSESET @iRunno = @iRunno + 1
DECLARE view_cursor CURSOR FORSELECT nameFROM DATABASENAAM.sys.viewsWHERE name like 'vwBR!_%' ESCAPE '!'
OPEN view_cursor
FETCH NEXT FROM view_cursorINTO @vViewName
WHILE @@FETCH_STATUS = 0BEGINSET @SQLString = N'SELECT @icount_recOUT = count(*)FROM DATABASENAAM..'+ @vViewName;SET @ParmDefinition = N'@icount_recOUT int OUTPUT';--print @SQLStringEXECUTE sp_executesql @SQLString, @ParmDefinition,@icount_recOUT= @iCount_rec OUTPUT;
SET @vcTableName = (SELECT BusinessRuleViewTableNameFROM DATABASENAAM..BusinessRuleViewsWHERE BusinessRuleViewName = @vViewName)
SET @SQLString = N'SELECT @icount_TotRecOUT = count(*)FROM DATABASENAAM..'+ @vcTableName ;SET @ParmDefinition = N'@icount_TotRecOUT int OUTPUT';--print @SQLStringEXECUTE sp_executesql @SQLString, @ParmDefinition,@icount_TotRecOUT = @iCount_TotRec OUTPUT;
--print @vViewNameSet @iTotRowCount = (SELECT count(*) FROMDATABASENAAM..BusinessRuleViewsWHERE BusinessRuleViewName = @vViewName)
INSERT INTO DATABASENAAM..BusinessRuleViewsCount(BusinessRuleViewID,BusinessRuleViewCountRunno, BusinessRuleViewCountDate,
BusinessRuleViewCountCount, BusinessRuleViewCountTotTableCount)SELECT BusinessRuleViewID , @iRunNo, getdate(),@iCount_rec, @iCount_TotRecFROM BusinessRuleViewsWHERE BusinessRuleViewName = @vViewName
FETCH NEXT FROM view_cursorINTO @vViewNameEND
CLOSE view_cursorDEALLOCATE view_cursorENDStep 5 - Schedule the Stored Procedure
Finally you schedule the call to the Stored Procedure. The best moment to call it is as soon as the ETL is done and all records are loaded.
Step 6 - Graphical display
Finally I have two reports that are built on the table. They show the counts per Business Rule, and you can drill down to the details.
If you have tips or comments, please leave a message!
Frequently asked questions
What is a business rule in a data warehouse?
A rule that applies to a company. Examples: a delivery date may not be before the order date, and a sofi number (Dutch social security number) must have 9 digits.
What do I do with records that do not meet a business rule?
You can throw them away, put them aside in a reject table, or just load them. This is a choice you make when you develop the data warehouse.
How do I measure the data quality of a data warehouse with business rules?
Make a view per business rule that gets the rows that do not meet the rule, and count those rows after every ETL run. Store the counts per day. That gives you a trend line of the quality.
How do I name the views for business rules in SQL Server?
Start with vwBR, followed by the table name and then the name of the business rule. For example vwBR_DimMedewerker_MeerDan40UrigContract. This keeps all views neatly together.
When do I schedule the stored procedure for the business rule counts?
As soon as the ETL is done and all records are loaded. The procedure then counts all views and writes the counts to the table BusinessRuleViewsCount.
Comments
Related blogs
Starting and stopping SQL Server Services
Query Active Directory
Analysis Services, Reporting and Excel 2007