-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStored Procedure to Load landing table CommDailyDetail.sql
More file actions
224 lines (198 loc) · 6.45 KB
/
Stored Procedure to Load landing table CommDailyDetail.sql
File metadata and controls
224 lines (198 loc) · 6.45 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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
USE [TempDataLake]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create PROCEDURE [commweb_landing].[pr_Load_CommDailyDetail]
AS
BEGIN
DECLARE @ProcessDate date, @ApplicationName varchar(500), @ProcessZone varchar(500), @PackageName varchar(500), @TaskName varchar(500),
@SrcTableName varchar(500), @TgtTableName varchar(500), @Comments varchar(500), @ProcessStatus varchar(500),
@StartTS datetime, @EndTS datetime, @RowCount int, @ProcessUser varchar(100)
SET @ProcessDate = GETDATE()
SET @ApplicationName = 'COMMWEB'
SET @ProcessZone = 'Curation'
SET @PackageName = NULL
SET @TaskName = NULL
SET @SrcTableName= 'GMPCommission.[SSRS].pr_GetCommissionTradeDetails'
SET @TgtTableName = '[commweb_landing].[CommDailyDetail]'
SET @ProcessStatus = 'Started'
SET @StartTS = GETDATE()
SET @ProcessUser = USER_NAME()
DECLARE @LoadID int
/*-- Get LoadID: */
IF EXISTS (SELECT * FROM tempdb.dbo.sysobjects WHERE id = object_id('tempdb.dbo.#tmpLoadId'))
BEGIN
DROP TABLE #tmpLoadId
END
CREATE TABLE #tmpLoadId ( LoadId int NULL )
INSERT INTO #tmpLoadId
EXEC RWL.[pr_Insert_PROCESS_LOG_OnBegin] @ProcessDate, @ApplicationName, @ProcessZone, @PackageName, @TaskName,
@SrcTableName, @TgtTableName, @ProcessStatus, @StartTS, @ProcessUser
SELECT @LoadID = LoadId FROM #tmpLoadId
-- Copy current date from COMMWEB.COMM_DAILY_DETAIL
DECLARE @UserID as varchar(100), @IACode as varchar(8), @ToIACode as varchar(8), @ProcessingDate as datetime,
@Type as int, @FilterOption as int, @FLAGS as varchar(500), @IncludeInactiveIACodes as bit
SET @UserID='Adelaide\mlam'
SET @IACode='0'
SET @ToIACode='0'
SELECT @ProcessingDate = GMPCommission.[payout].[udf_GetMaxProcessingDate]() -- Get most recent date from GMPCommission
SET @Type=2 --1:daily, 2:MTD
SET @FilterOption=0 --0: no filter, 1:ticketCharge<>0
SET @IncludeInactiveIACodes = 0
SET @FLAGS='Trades - FI $50 to $99.99,Trades - Insurance,Trades - New Issues,Trades - Other,Trades - PMA NI,Trades - Trades No Payout,Trades - Regular Trades,Trades - PMA No Payout,Trades - SMA No Payout,Fees - IAA,Fees - PMA,Fees - SMA,Fees - UMA'
BEGIN TRY
TRUNCATE TABLE [commweb_landing].[CommDailyDetail]
-- Create temporary table to store stored proc results --
IF EXISTS (SELECT * FROM tempdb.dbo.sysobjects WHERE id = object_id('tempdb.dbo.#tmpCDRDetails'))
BEGIN
DROP TABLE #tmpCDRDetails
END
CREATE TABLE #tmpCDRDetails
(
[IaId] [varchar](10) NULL,
[ToIaId] [varchar](10) NULL,
[TradeNumber] [varchar](20) NULL,
[TradeDate] [datetime] NULL,
[ProcessingDate] [datetime] NULL,
[OrderNumber] [int] NULL,
[OrderDate] [datetime] NULL,
[SettlementDate] [datetime] NULL,
[AccountId] [varchar](20) NULL,
[Port_Type] [varchar](10) NULL,
[ClientName] [varchar](100) NULL,
[TradeRR] [varchar](20) NULL,
[Flag] [varchar](20) NULL,
[BuySell] [varchar](5) NULL,
[Quantity] [decimal](18, 4) NULL,
[SymbolDesc] [varchar](200) NULL,
[Cusip] [varchar](20) NULL,
[SecClass] [int] NULL,
[SecTypeClass] [varchar](10) NULL,
[Price] [decimal](18, 4) NULL,
[GrossAmount] [decimal](18, 4) NULL,
[GrossCommission] [decimal](18, 4) NULL,
[GrossCommissionCAD] [decimal](18, 4) NULL,
[Currency] [varchar](3) NULL,
[MCGrossCommissionCAD] [decimal](18, 4) NULL,
[MCGrossCommission] [decimal](18, 4) NULL,
[SCGrossCommissionCAD] [decimal](18, 4) NULL,
[SCGrossCommission] [decimal](18, 4) NULL,
[GrossSplitPct] [decimal](18, 4) NULL,
[PayoutPct] [decimal](18, 4) NULL,
[NetCommission] [decimal](18, 4) NULL,
[TicketCharge] [decimal](18, 4) NULL,
[NetToRR] [decimal](18, 4) NULL,
[IsDiscountFree] [varchar](2) NULL,
[CommPercent] [varchar](20) NULL, -- convert to decimal in landing
[GroupSeq] [int] NULL,
[RowOrder] [int] NULL,
[PMAMZExcessFee] [decimal](18, 4) NULL,
[PayoutStatus] [varchar](2) NULL,
[FamilyHHID] [int] NULL
) ON [PRIMARY]
INSERT INTO [commweb_landing].[CommDailyDetail] (
[IaId]
,[ToIaId]
,[TradeNumber]
,[TradeDate]
,[ProcessingDate]
,[BusinessDate]
,[OrderNumber]
,[OrderDate]
,[SettlementDate]
,[AccountId]
,[Port_Type]
,[ClientName]
,[TradeRR]
,[Flag]
,[BuySell]
,[Quantity]
,[SymbolDesc]
,[Cusip]
,[SecClass]
,[SecTypeClass]
,[Price]
,[GrossAmount]
,[GrossCommission]
,[GrossCommissionCAD]
,[Currency]
,[MCGrossCommissionCAD]
,[MCGrossCommission]
,[SCGrossCommissionCAD]
,[SCGrossCommission]
,[GrossSplitPct]
,[PayoutPct]
,[NetCommission]
,[TicketCharge]
,[NetToRR]
,[IsDiscountFree]
,[CommPercent]
,[GroupSeq]
,[RowOrder]
,[PMAMZExcessFee]
,[PayoutStatus]
,[FamilyHHID]
)
SELECT
[IaId]
,[ToIaId]
,[TradeNumber]
,[TradeDate]
,[ProcessingDate]
,@ProcessingDate as BusinessDate
,[OrderNumber]
,[OrderDate]
,[SettlementDate]
,[AccountId]
,[Port_Type]
,[ClientName]
,[TradeRR]
,[Flag]
,[BuySell]
,[Quantity]
,[SymbolDesc]
,[Cusip]
,[SecClass]
,[SecTypeClass]
,[Price]
,[GrossAmount]
,[GrossCommission]
,[GrossCommissionCAD]
,[Currency]
,[MCGrossCommissionCAD]
,[MCGrossCommission]
,[SCGrossCommissionCAD]
,[SCGrossCommission]
,CAST([GrossSplitPct] as decimal(8,4))
,[PayoutPct]
,[NetCommission]
,[TicketCharge]
,[NetToRR]
,[IsDiscountFree]
,CASE WHEN [CommPercent]='' THEN 0 ELSE CONVERT(decimal(8,4), [CommPercent]) END
,[GroupSeq]
,[RowOrder]
,[PMAMZExcessFee]
,[PayoutStatus]
,[FamilyHHID]
from #tmpCDRDetails
EXEC GMPCommission.[SSRS].pr_GetCommissionTradeDetails @UserID, @IACode, @ToIACode, @ProcessingDate, @Type, @FilterOption, @FLAGS, @IncludeInactiveIACodes
--from GMPCommission.[SSRS].pr_GetCommissionTradeDetails (@UserID, @IACode, @ToIACode, @ProcessingDate, @Type, @FilterOption, @FLAGS, @IncludeInactiveIACodes)
SELECT @RowCount=Count(*) FROM [commweb_landing].[CommDailyDetail] WHERE [BusinessDate]=(SELECT MAX([BUSINESSDATE]) FROM [commweb_landing].[CommDailyDetail])
IF @RowCount > 0 -- Load is successful
SET @ProcessStatus='Completed Successfully'
SET @Comments='SUCCESS: [commweb_landing].[CommDailyDetail] Loaded'
SET @EndTS = GETDATE()
EXEC RWL.[pr_Update_PROCESS_LOG_OnEnd] @LoadId, @ProcessStatus, @Comments, @EndTS, @RowCount
END TRY
BEGIN CATCH
-- If there is an error
SET @ProcessStatus = 'Error while loading'
SET @Comments = 'ErrNum=' + CAST(ERROR_NUMBER() AS varchar(20)) + ' ' + ERROR_MESSAGE()
SET @EndTS = GETDATE()
EXEC RWL.[pr_Update_PROCESS_LOG_OnFailure] @LoadId, @ProcessStatus, @Comments, @EndTS
END CATCH
END
GO