-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSQL_Transform.sql
115 lines (83 loc) · 3.39 KB
/
SQL_Transform.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
USE Database Name;
/*BASIC CLEAN UP - DROP BLANK ROWS, REFORMAT variable12*/
IF OBJECT_ID('???.TableName', 'U') IS NOT NULL DROP TABLE [???].[TableName2];
IF OBJECT_ID('???.TableName', 'U') IS NOT NULL SELECT * INTO [???].[TableName2] FROM ???.TableName;
ALTER TABLE [dbo].[table2] ALTER COLUMN [variable1] VARCHAR(30);
ALTER TABLE [dbo].[table2] ALTER COLUMN [variable2] VARCHAR(30);
ALTER TABLE [dbo].[table2] ALTER COLUMN [variable3] VARCHAR(30);
ALTER TABLE [dbo].[table2] ALTER COLUMN [variable4] VARCHAR(30);
ALTER TABLE [dbo].[table2] ALTER COLUMN [variable5] VARCHAR(30);
ALTER TABLE [dbo].[table2] ALTER COLUMN [variable6] VARCHAR(30);
ALTER TABLE [dbo].[table2] ALTER COLUMN [variable7] VARCHAR(30);
ALTER TABLE [dbo].[table2] ALTER COLUMN [variable8] VARCHAR(30);
UPDATE [???].[TableName2]
SET variable10 = CASE WHEN variable10 = '11' THEN '1'
WHEN variable10 = '22' THEN '2'
WHEN variable10 = '33' THEN '3'
WHEN variable10 = '44' THEN '4'
WHEN variable10 = '55' THEN '5'
WHEN variable10 = '66' THEN '6'
WHEN variable10 = '77' THEN '7'
ELSE variable10 END;
UPDATE [???].[TableName2]
SET variable9 = '??'
WHERE variable9 in ('Y', 'X') and variable10 in ('1', '2', '3', '4', '5');
DELETE FROM [???].[TableName2]
WHERE variable154 in ('aaa', 'bbb', 'ccc');
DELETE FROM [???].[TableName2]
WHERE variable9 in ('??', '??') and variable10 = '0' and variable12 = '0';
DELETE FROM [???].[TableName2]
WHERE variable10 = '0' and variable14 = 0 and variable20 = 0 and variable90 = 0;
/* CLEAN UP variable12s */
UPDATE [???].[TableName2]
SET variable12 = 'a'
WHERE variable12 = 'b';
UPDATE [???].[TableName2]
SET variable12 = 'c'
WHERE variable12 = 'd';
UPDATE [???].[TableName2]
SET variable12 = 'e'
WHERE variable12 = 'f';
/* Update variable10 */
UPDATE [???].[TableName2]
SET variable10 = 'g', variable11 = 'h'
WHERE variable10 ='i';
UPDATE [???].[TableName2]
SET variable10 = 'j', variable11 = 'k'
WHERE variable10 = 'l';
UPDATE [???].[TableName2]
SET variable10 = 'm', variable11 = 'n'
WHERE variable10 = 'o' and variable11 = 'p';
/* Update variable9 */
UPDATE [???].[TableName2]
SET variable9 = 'q'
WHERE variable10 = 'r';
UPDATE [???].[TableName2]
SET variable9 = 's'
WHERE variable10 in ('t', 'u', 'v');
UPDATE [???].[TableName2]
SET variable9 = 'w'
WHERE variable10 in ('x', 'y');
/* Rebuild variable1 Fields */
UPDATE [???].[TableName2]
SET variable3 = [variable8];
UPDATE [???].[TableName2]
SET variable4 = CASE WHEN [variable8] < variable153 then 'Ok'
ELSE [variable8] END;
UPDATE [???].[TableName2]
SET variable5 = variable1(variable12, '\', [variable153]);
UPDATE [???].[TableName2]
SET variable6 = variable1(variable10,variable11,'\',[variable153]);
UPDATE [???].[TableName2]
SET variable7 = variable1([variable8],'\',variable9, '\', variable10,'\',variable11,'\',variable12);
/* Rebuild Fields That Didn't Exist Prior to Certain Files*/
UPDATE [???].[TableName2]
SET variable155 = CASE WHEN [variable] = variable153 then 0
ELSE variable37 + variable63 - variable59 END;
UPDATE [???].[TableName2]
SET variable156 = CASE WHEN [variable] = variable153 then 0
ELSE variable38 + variable64 - variable60 END
/* Rebuild Indexes */
CREATE INDEX IX_variable1_variable2 ON [dbo].[table2] (variable1, variable2);
CREATE INDEX IX_variable3_variable4 ON [dbo].[table2] (variable3, variable4);
CREATE INDEX IX_variable5_variable6 ON [dbo].[table2] (variable5, variable6)