-
Notifications
You must be signed in to change notification settings - Fork 32
/
DataViewTables.sql
234 lines (204 loc) · 13.9 KB
/
DataViewTables.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
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
224
225
226
227
228
229
230
231
232
233
234
/**
* Copyright (c) 2013 Oculus Info Inc.
* http://www.oculusinfo.com/
*
* Released under the MIT License.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
-- -----------------------------
-- Influent Data Views 1.2 DRAFT
-- -----------------------------
--
-- FINANCIAL FLOW - ALL
-- Used to build the aggregate flow diagrams
--
-- FromEntityId - entity UID that is the source of the transactions
-- FromEntityType - type of src entity: O = owner summary, A = account, S = cluster summary entity
-- ToEntityId - entity UID that is the target of the transactions
-- ToEntityType - type of dst entity: O = owner summary, A = account, S = cluster summary entity
-- FirstTransaction - datetime of first transaction
-- LastTransaction - datetime of last transaction
-- Amount - aggregate amount
--
create table FinFlow (FromEntityId varchar(100), FromEntityType varchar(1), ToEntityId varchar(100), ToEntityType varchar(1), FirstTransaction datetime, LastTransaction datetime, Amount float);
create table FinEntity(EntityId varchar(100), InboundDegree int, UniqueInboundDegree int, OutboundDegree int, UniqueOutboundDegree int);
--
-- FINANCIAL FLOW - AGGREGATED BY TIME
-- Used to build the aggregate flow diagrams (aggregated by time)
-- and used to build the highlighted sub-section of the time series charts on entities.
--
-- FromEntityId - entity UID that is the source of the transactions
-- FromEntityType - type of src entity: O = owner summary, A = account, S = cluster summary entity
-- ToEntityId - entity UID that is the target of the transactions
-- ToEntityType - type of dst entity: O = owner summary, A = account, S = cluster summary entity
-- Amount - aggregate amount for this time period
-- Date - start of the time period
--
create table FinFlowDaily (FromEntityId varchar(100), FromEntityType varchar(1), ToEntityId varchar(100), ToEntityType varchar(1), Amount float, PeriodDate datetime);
create table FinFlowWeekly (FromEntityId varchar(100), FromEntityType varchar(1), ToEntityId varchar(100), ToEntityType varchar(1), Amount float, PeriodDate datetime);
create table FinFlowMonthly (FromEntityId varchar(100), FromEntityType varchar(1), ToEntityId varchar(100), ToEntityType varchar(1), Amount float, PeriodDate datetime);
create table FinFlowQuarterly (FromEntityId varchar(100), FromEntityType varchar(1), ToEntityId varchar(100), ToEntityType varchar(1), Amount float, PeriodDate datetime);
create table FinFlowYearly (FromEntityId varchar(100), FromEntityType varchar(1), ToEntityId varchar(100), ToEntityType varchar(1), Amount float, PeriodDate datetime);
--
-- FINANCIAL ENTITY SUMMARY
-- Used to build the time series charts on entities (aggregated by time).
--
-- EntityId - entity UID
-- Date - start of the time period
-- InboundAmount - aggregate credits for this time period
-- InboundDegree - unique inbound transations by entity
-- OutboundAmount - aggregate debits for this time period
-- OutboundDegree - unique outbound transations by entity
-- Balance - aggregate credits - debits up until this time period
--
create table FinEntityDaily (EntityId varchar(100), PeriodDate datetime, InboundAmount float, InboundDegree int, OutboundAmount float, OutboundDegree int, Balance float);
create table FinEntityWeekly (EntityId varchar(100), PeriodDate datetime, InboundAmount float, InboundDegree int, OutboundAmount float, OutboundDegree int, Balance float);
create table FinEntityMonthly (EntityId varchar(100), PeriodDate datetime, InboundAmount float, InboundDegree int, OutboundAmount float, OutboundDegree int, Balance float);
create table FinEntityQuarterly (EntityId varchar(100), PeriodDate datetime, InboundAmount float, InboundDegree int, OutboundAmount float, OutboundDegree int, Balance float);
create table FinEntityYearly (EntityId varchar(100), PeriodDate datetime, InboundAmount float, InboundDegree int, OutboundAmount float, OutboundDegree int, Balance float);
--
-- CLUSTER SUMMARY
-- Used to summarize an entity with a large number of associated entities (e.g. account owner with a large number of accounts)
-- It is up to each application to determine what cluster summaries to generate based on the size of data
--
-- EntityId - entity UID of cluster entity
-- Property - name of summary property
-- Tag - Property_Tag to associate with property
-- Type - the data type of property value (string, double, float, int, date)
-- Value - the string representation of the property value
-- Stat - an associated stat for the propety value such as frequency or weight
--
create table ClusterSummary (EntityId varchar(100), Property varchar(50), Tag varchar(50), Type varchar(50), Value varchar(200), Stat float);
--
-- CLUSTER SUMMARY MEMBERS
-- Used to keep track of entities that are members of a cluster summary
-- It is up to each application to determine what cluster summaries to generate based on the size of data
--
-- SummaryId - UID of cluster summary
-- EntityId - member entity UID
--
create table ClusterSummaryMembers (SummaryId varchar(100), EntityId varchar(100));
--
-- DATA VIEW DRIVERS
-- These scripts will populate the data views above.
--
-- Step 1. Modify this to pull data from your raw data. Add any transactions to cluster summaries as well.
--
insert into FinFlowDaily
SELECT [source_id], 'A', [dest_id], 'A', sum([amount]), convert(varchar(50), [dt], 101)
FROM YOUR_RAW_DATA
group by [source_id], [dest_id], convert(varchar(50), [dt], 101)
--
-- Step 2. The rest of the script will collect data from FinFlowDaily.
-- Execute the rest of this script "as-is".
--
-- build the rest of the FinFlow aggregations
insert into FinFlowWeekly
select FromEntityId, FromEntityType, ToEntityId, ToEntityType, sum(Amount), CONVERT(varchar(50), (DATEADD(dd, @@DATEFIRST - DATEPART(dw, PeriodDate) - 6, PeriodDate)), 101)
from FinFlowDaily
group by FromEntityId, FromEntityType, ToEntityId, ToEntityType, CONVERT(varchar(50), (DATEADD(dd, @@DATEFIRST - DATEPART(dw, PeriodDate) - 6, PeriodDate)), 101);
insert into FinFlowMonthly
select FromEntityId, FromEntityType, ToEntityId, ToEntityType, sum(Amount), CONVERT(varchar(50), convert(varchar(4), DATEPART(yyyy, PeriodDate)) + '/' + convert(varchar(2), DATEPART(mm, PeriodDate)) + '/01', 101)
from FinFlowDaily
group by FromEntityId, FromEntityType, ToEntityId, ToEntityType, CONVERT(varchar(50), convert(varchar(4), DATEPART(yyyy, PeriodDate)) + '/' + convert(varchar(2), DATEPART(mm, PeriodDate)) + '/01', 101);
insert into FinFlowQuarterly
select FromEntityId, FromEntityType, ToEntityId, ToEntityType, sum(Amount), CONVERT(varchar(50), convert(varchar(4), DATEPART(yyyy, PeriodDate)) + '/' + case when DATEPART(q, PeriodDate)=1 then '01' when DATEPART(q, PeriodDate)=2 then '04' when DATEPART(q, PeriodDate)=3 then '07' when DATEPART(q, PeriodDate)=4 then '010' end + '/01', 101)
from FinFlowMonthly
group by FromEntityId, FromEntityType, ToEntityId, ToEntityType, CONVERT(varchar(50), convert(varchar(4), DATEPART(yyyy, PeriodDate)) + '/' + case when DATEPART(q, PeriodDate)=1 then '01' when DATEPART(q, PeriodDate)=2 then '04' when DATEPART(q, PeriodDate)=3 then '07' when DATEPART(q, PeriodDate)=4 then '010' end + '/01', 101);
insert into FinFlowYearly
select FromEntityId, FromEntityType, ToEntityId, ToEntityType, sum(Amount), CONVERT(varchar(50), convert(varchar(4), DATEPART(yyyy, PeriodDate)) + '/01/01', 101)
from FinFlowMonthly
group by FromEntityId, FromEntityType, ToEntityId, ToEntityType, CONVERT(varchar(50), convert(varchar(4), DATEPART(yyyy, PeriodDate)) + '/01/01', 101);
-- create FinFlow indices
create index ix_ffd_from on FinFlowDaily (FromEntityId, PeriodDate, ToEntityId, Amount);
create index ix_ffd_to on FinFlowDaily (ToEntityId, PeriodDate, FromEntityId, Amount);
create index ix_ffw_from on FinFlowWeekly (FromEntityId, PeriodDate, ToEntityId, Amount);
create index ix_ffw_to on FinFlowWeekly (ToEntityId, PeriodDate, FromEntityId, Amount);
create index ix_ffm_from on FinFlowMonthly (FromEntityId, PeriodDate, ToEntityId, Amount);
create index ix_ffm_to on FinFlowMonthly (ToEntityId, PeriodDate, FromEntityId, Amount);
create index ix_ffq_from on FinFlowQuarterly (FromEntityId, PeriodDate, ToEntityId, Amount);
create index ix_ffq_to on FinFlowQuarterly (ToEntityId, PeriodDate, FromEntityId, Amount);
create index ix_ffy_from on FinFlowYearly (FromEntityId, PeriodDate, ToEntityId, Amount);
create index ix_ffy_to on FinFlowYearly (ToEntityId, PeriodDate, FromEntityId, Amount);
-- build FinFlow
insert into FinFlow
select FromEntityId, FromEntityType, ToEntityId, ToEntityType, min(PeriodDate), max(PeriodDate), sum(Amount)
from FinFlowDaily
group by FromEntityId, FromEntityType, ToEntityId, ToEntityType;
create index ix_ff_to_from on FinFlow (ToEntityId, FromEntityId);
create index ix_ff_from_to on FinFlow (FromEntityId, ToEntityId);
-- build FinEntity
insert into FinEntity
select EntityId, sum(inboundDegree), sum(uniqueInboundDegree), sum(outboundDegree), sum(uniqueOutboundDegree)
from (
select FromEntityId as EntityId, 0 as inboundDegree, 0 as uniqueInboundDegree, count(ToEntityId) as outboundDegree, count( distinct ToEntityId ) as uniqueOutboundDegree
from FinFlowDaily
where ToEntityType = 'A'
group by FromEntityId
union
select ToEntityId as EntityId, count(FromEntityId) as inboundDegree, count( distinct FromEntityId ) as uniqueInboundDegree, 0 as outboundDegree, 0 as uniqueOutboundDegree
from FinFlowDaily
where FromEntityType = 'A'
group by ToEntityId
) q
group by EntityId
create index ix_ff_id on FinEntity (EntityId);
-- build FinEntityDaily
create table temp_ids (Entity varchar(100));
create index tids on temp_ids (Entity);
insert into temp_ids
select distinct FromEntityId
from FinFlowYearly
union
select distinct ToEntityId
from FinFlowYearly;
insert into FinEntityDaily select Entity, PeriodDate,
sum(case when ToEntityId = Entity and FromEntityType = 'A' then Amount else 0 end),
sum(case when ToEntityId = Entity and FromEntityType = 'A' then 1 else 0 end), -- calculate inbound degree
sum(case when FromEntityId = Entity and ToEntityType = 'A' then Amount else 0 end),
sum(case when FromEntityId = Entity and ToEntityType = 'A' then 1 else 0 end), -- calculate outbound degree
0 -- TODO calculate balance
from temp_ids
join FinFlowDaily on FromEntityId = Entity or ToEntityId = Entity
group by Entity, PeriodDate;
-- cleanup
drop table temp_ids;
-- build the rest of the FinEntity aggregations
insert into FinEntityWeekly
select EntityId, CONVERT(varchar(50), (DATEADD(dd, @@DATEFIRST - DATEPART(dw, PeriodDate) - 6, PeriodDate)), 101), sum(InboundAmount), sum(InboundDegree), sum(OutboundAmount), sum(OutboundDegree), 0
from FinEntityDaily
group by EntityId, CONVERT(varchar(50), (DATEADD(dd, @@DATEFIRST - DATEPART(dw, PeriodDate) - 6, PeriodDate)), 101);
insert into FinEntityMonthly
select EntityId, CONVERT(varchar(50), convert(varchar(4), DATEPART(yyyy, PeriodDate)) + '/' + convert(varchar(2), DATEPART(mm, PeriodDate)) + '/01', 101), sum(InboundAmount), sum(InboundDegree), sum(OutboundAmount), sum(OutboundDegree), 0
from FinEntityDaily
group by EntityId, CONVERT(varchar(50), convert(varchar(4), DATEPART(yyyy, PeriodDate)) + '/' + convert(varchar(2), DATEPART(mm, PeriodDate)) + '/01', 101);
insert into FinEntityQuarterly
select EntityId, CONVERT(varchar(50), convert(varchar(4), DATEPART(yyyy, PeriodDate)) + '/' + case when DATEPART(q, PeriodDate)=1 then '01' when DATEPART(q, PeriodDate)=2 then '04' when DATEPART(q, PeriodDate)=3 then '07' when DATEPART(q, PeriodDate)=4 then '010' end + '/01', 101), sum(InboundAmount), sum(InboundDegree), sum(OutboundAmount), sum(OutboundDegree), 0
from FinEntityMonthly
group by EntityId, CONVERT(varchar(50), convert(varchar(4), DATEPART(yyyy, PeriodDate)) + '/' + case when DATEPART(q, PeriodDate)=1 then '01' when DATEPART(q, PeriodDate)=2 then '04' when DATEPART(q, PeriodDate)=3 then '07' when DATEPART(q, PeriodDate)=4 then '010' end + '/01', 101);
insert into FinEntityYearly
select EntityId, CONVERT(varchar(50), convert(varchar(4), DATEPART(yyyy, PeriodDate)) + '/01/01', 101), sum(InboundAmount), sum(InboundDegree), sum(OutboundAmount), sum(OutboundDegree), 0
from FinEntityQuarterly
group by EntityId, CONVERT(varchar(50), convert(varchar(4), DATEPART(yyyy, PeriodDate)) + '/01/01', 101);
create index ix_fed on FinEntityDaily (EntityId, PeriodDate, InboundAmount, OutboundAmount);
create index ix_few on FinEntityWeekly (EntityId, PeriodDate, InboundAmount, OutboundAmount);
create index ix_fem on FinEntityMonthly (EntityId, PeriodDate, InboundAmount, OutboundAmount);
create index ix_feq on FinEntityQuarterly (EntityId, PeriodDate, InboundAmount, OutboundAmount);
create index ix_fey on FinEntityYearly (EntityId, PeriodDate, InboundAmount, OutboundAmount);
create index ix_csum on ClusterSummary (EntityId);
create index ix_cmem on ClusterSummaryMembers (SummaryId);