You are on page 1of 4

USE [Insurance]

GO
/****** Object: StoredProcedure [dbo].[Data_Cleansing]
12/25/2013 16:55:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:
<Author,,Name>
-- Create date: <Create Date,,>
-- Description:
<Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[Data_Cleansing]
@No_YYYY [numeric](18, 0),
@No_MM [numeric](18, 0)
AS
BEGIN

Script Date:

delete from [Insurance].[dbo].[Companies_Data_Clean]


where [No_YYYY] = @No_YYYY
and [No_MM] = @No_MM
insert into [Insurance].[dbo].[Companies_Data_Clean]
select [No_YYYY]
,[No_MM]
,[Company_Id]
,[Rep_Id]
,[Line_Id]
,[Col_Id]
,[Data_Figure]
,[Snapshot_Date]
,[Data_Figure]
from [Insurance].[dbo].[Companies_Data]
where [No_YYYY] = @No_YYYY
and [No_MM] = @No_MM
------------delete 1 row for company 22 if not exists
Delete FROM [Insurance].[dbo].[Companies_Data_Clean]
where no_yyyy =2013
and no_mm = 9
and col_id =1
and company_id = 22
and line_id =24
and rep_id =5

------------delete 1 row for company 25 if not exists


Delete FROM [Insurance].[dbo].[Companies_Data_Clean]
where no_yyyy =2013

and
and
and
and
and

no_mm = 9
col_id =1
company_id = 25
line_id =24
rep_id =5

------------Add 1 row for company 22 if not exists


if (not exists
(SELECT [No_YYYY]
,[No_MM]
,[Company_Id]
,[Rep_Id]
,[Line_Id]
,[Col_Id]
,[Data_Figure]
,[Snapshot_Date]
,[Data_Figure_Clean]
FROM [Insurance].[dbo].[Companies_Data_Clean]
where no_yyyy =2013
and no_mm = 9
and col_id =1
and company_id = 22
and line_id =24
and rep_id =5))
BEGIN
INSERT

INTO [Insurance].[dbo].[Companies_Data_Clean]
([No_YYYY]
,[No_MM]
,[Company_Id]
,[Rep_Id]
,[Line_Id]
,[Col_Id]
,[Data_Figure]
,[Snapshot_Date]
,[Data_Figure_Clean])
VALUES
(2013,9,22,5,24,1,200000,null,200000)

end
-----------------Add one row for company 23 if not exists

/****** Script for SelectTopNRows command from SSMS


if (not exists
(SELECT [No_YYYY]
,[No_MM]
,[Company_Id]
,[Rep_Id]
,[Line_Id]
,[Col_Id]
,[Data_Figure]
,[Snapshot_Date]

******/

,[Data_Figure_Clean]
FROM [Insurance].[dbo].[Companies_Data_Clean]
where no_yyyy =2013
and no_mm = 9
and col_id =1
and company_id = 25
and line_id =24
and rep_id =5))
BEGIN
INSERT

INTO [Insurance].[dbo].[Companies_Data_Clean]
([No_YYYY]
,[No_MM]
,[Company_Id]
,[Rep_Id]
,[Line_Id]
,[Col_Id]
,[Data_Figure]
,[Snapshot_Date]
,[Data_Figure_Clean])
VALUES
(2013,9,25,5,24,1,100000,null,100000)

end

update [Insurance].[dbo].[Companies_Data_Clean]
set data_figure = 100000 , data_figure_clean =100000
where rep_id =5
and company_id = 25
and line_id = 25
and no_yyyy =2013
and no_mm = 9
and col_id =1
update [Insurance].[dbo].[Companies_Data_Clean]
set data_figure = 200000 , data_figure_clean =200000
where rep_id =5
and company_id = 22
and line_id = 25
and no_yyyy =2013
and no_mm = 9
and col_id =1

---- Data Cleansing Queries: Insurance

-------

--Concatenate Year and Month to create a DateTime for 28th of the Month
update Companies_data_clean set Snapshot_Date =
cast(rtrim(ltrim(No_YYYY)) +'-'+ rtrim(ltrim(No_MM)) +'-'+ '28' AS
DATETIME)

----Changing the Column Name in Report_Columns to some other Name:


Update [Insurance].[dbo].[Report_Columns]set Col_Name = 'Direct Sales'
where ltrim(rtrim(Col_Name)) = 'Number of Policies Written - Direct
Sales' and Rep_Id = 34
;
---Remove Commas
Update Companies_data_clean
Set Data_Figure_Clean = replace(ltrim(rtrim(Data_Figure_Clean)), ',',
'')
-----Remove Brackets to ve Sign
Update Companies_data_clean Set Data_Figure_Clean =
replace(ltrim(rtrim(Data_Figure_Clean)), '(', '-')
Update Companies_data_clean Set Data_Figure_Clean =
replace(ltrim(rtrim(Data_Figure_Clean)), ')', '')
Update Companies_data_clean Set Data_Figure_Clean =
replace(ltrim(rtrim(Data_Figure_Clean)), '%', '')
Update Insurance.dbo.Report_Lines
Set Line_Name = ltrim(rtrim(Line_name))
Update Insurance.dbo.Report_Columns
Set Col_Name = ltrim(rtrim(Col_name))
----Remove Percentage
update companies_data_clean
set Data_Figure_Clean = replace(ltrim(rtrim(Data_Figure_Clean)),'%','')

---Remove '--'
Update Companies_data_clean
Set Data_Figure_Clean = replace(ltrim(rtrim(Data_Figure_Clean)), '--',
'')
----Update Some Records

END

You might also like