Repository navigation
Expand file tree
/
Copy pathFeatureEngineer.sql
More file actions
68 lines (59 loc) · 2.4 KB
/
Copy pathFeatureEngineer.sql
File metadata and controls
68 lines (59 loc) · 2.4 KB
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
/* assume risk tables and the table storing column names (named 'sql_risk_var') have been created in step3 */
/* procedure to do feature engineering */
use [OnlineFraudDetection]
go
set ansi_nulls on
go
set quoted_identifier on
go
DROP PROCEDURE IF EXISTS dbo.FeatureEngineer
GO
create procedure dbo.FeatureEngineer @table nvarchar(max)
as
begin
/* set binary variables */
declare @sql_bin nvarchar(max)
set @sql_bin = 'update ' + @table +
'
set is_highAmount = case when cast(transactionAmountUSD as float) > 150 then ''TRUE'' else ''FALSE'' end,
acct_billing_address_mismatchFlag = case when paymentBillingAddress = accountAddress then ''FALSE'' else ''TRUE'' end,
acct_billing_postalCode_mismatchFlag = case when paymentBillingPostalCode = accountPostalCode then ''FALSE'' else ''TRUE'' end,
acct_billing_country_mismatchFlag = case when paymentBillingCountryCode = accountCountry then ''FALSE'' else ''TRUE'' end,
acct_billing_name_mismatchFlag = case when paymentBillingName = accountOwnerName then ''FALSE'' else ''TRUE'' end,
acct_shipping_address_mismatchFlag = case when shippingAddress = accountAddress then ''FALSE'' else ''TRUE'' end,
acct_shipping_postalCode_mismatchFlag = case when shippingPostalCode = accountPostalCode then ''FALSE'' else ''TRUE'' end,
acct_shipping_country_mismatchFlag = case when shippingCountry = accountCountry then ''FALSE'' else ''TRUE'' end,
shipping_billing_address_mismatchFlag = case when shippingAddress = paymentBillingAddress then ''FALSE'' else ''TRUE'' end,
shipping_billing_postalCode_mismatchFlag = case when shippingPostalCode = paymentBillingPostalCode then ''FALSE'' else ''TRUE'' end,
shipping_billing_country_mismatchFlag = case when shippingCountry = paymentBillingCountryCode then ''FALSE'' else ''TRUE'' end
from
' + @table
--print @sql_bin
exec sp_executesql @sql_bin
/* assign risk */
declare @sql_assign nvarchar(max)
set @sql_assign =
'
DECLARE @name_1 NVARCHAR(100)
DECLARE @name_2 NVARCHAR(100)
DECLARE @getname CURSOR
SET @getname = CURSOR FOR
SELECT var_names,
table_names
FROM sql_risk_var
OPEN @getname
FETCH NEXT
FROM @getname INTO @name_1,@name_2
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC AssignRisk @name_1,'+ '''' + @table + ''',@name_2
EXEC FillNA @name_1,@name_2,'+ '''' + @table + '''
FETCH NEXT
FROM @getname INTO @name_1, @name_2
END
CLOSE @getname
DEALLOCATE @getname
'
--print @sql_assign
exec sp_executesql @sql_assign
end