Repository navigation
Expand file tree
/
Copy pathinput.sql
More file actions
266 lines (256 loc) · 10.1 KB
/
Copy pathinput.sql
File metadata and controls
266 lines (256 loc) · 10.1 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
select
/* Core values to pull for the agent results */
c."npn",
c."agent-name",
/* Broker */
c.broker,
c."broker-master-id",
c.parent,
c."parent-master-id",
c.association,
c."association-master-id",
c."broker-cb-domain",
c."broker-general-agent-flag",
c."is-minor-broker-flag",
c."broker-master-office-id",
c."office-name",
c."office-address-line-1",
c."office-address-line-2",
c."office-address-city",
c."office-address-state",
c."office-address-zip",
c."office-address-county",
c."office-latitude",
c."office-longitude",
c."prepared-email-business-type",
c."modeled-email-flag",
c."primary_phone",
c."eligible-for-qualification",
c."linkedin-urn",
c."linkedin-url",
c."linkedin-refresh-date",
c."linkedin-location-city",
c."linkedin-location-state",
c."linkedin-location-top-zip",
c."linkedin-location-longitude",
c."linkedin-location-latitude",
c."linkedin-about",
c."linkedin-website",
c."linkedin-headline",
c."linkedin-followers",
c."linkedin-full_name",
c."linkedin-connections",
c."linkedin-experience-affiliation",
c."linkedin-experience-company",
c."linkedin-current-title",
c."linkedin-experience-location",
c."linkedin-experience-start",
c."linkedin-experience-end",
c."linkedin-started-with-company-months-ago",
c."linkedin-title-started-months-ago",
/* Custom fields
Must be aggregated to the grouping level, use array_agg generally */
array_agg(distinct c."lincoln-broker-office")
filter (where c."lincoln-broker-office" is not null) as "lincoln-broker-office",
array_agg(distinct c."lincoln-broker-region")
filter (where c."lincoln-broker-region" is not null) as "lincoln-broker-region",
/* Inject all aggregate clauses here */
/* Take the minimum general agent flag, which should be showing 'General Agent' before 'Not General Agent' */
min(c."general-agent-flag") as "general-agent-flag",
/* Agent aggregate stats */
count(distinct c."company-id")::double precision as count_employers,
count(*)::double precision as count_plans,
sum(case
when c."modeled-broker-flag" = 'Yes'
then 1
end)::double precision as count_modeled_plans,
avg(case
when c."competitive-status" = 'expansion'
then 1
when c."competitive-status" = 'contraction'
then -1
end)::double precision as expansion_vs_contraction_plan_count_skew,
round(avg(c."years-relationship")::double precision) as average_years_relationship_with_employers,
mode() within group (order by c.role) as most_common_agent_role,
sum(case
when c."general-agent-flag" = 'General Agent'
then 1
end)::double precision /
count(*)::double precision as ratio_of_general_agent_activity,
avg(c."attributed-premium")::double precision as avg_plan_attributed_premium,
sum(c."attributed-premium")::double precision as sum_plan_attributed_premium,
sum(c.commissions)::double precision as sum_plan_commissions,
avg(c."commission-rate")::double precision as avg_plan_commission_rate,
sum(c.fees)::double precision as sum_plan_fees,
avg(c."fee-rate")::double precision as avg_plan_fee_rate,
sum(c."total-compensation")::double precision as sum_plan_total_compensation,
avg(c."total-compensation-rate")::double precision as avg_plan_total_compensation_rate,
avg(c."total-compensation-share")::double precision as avg_plan_total_compensation_share,
mode() within group (order by c."compensation-rate-category") as most_common_compensation_rate_category,
/* Plan aggregate stats */
array_agg(distinct c."full-state")
filter (where c."full-state" is not null) as all_plan_states,
public.array_union(c.products)
filter (where c.products is not null) as all_plan_products,
array_agg(distinct c."carrier-name")
filter (where c."carrier-name" is not null) as all_carriers,
sum(c."employee-count")::double precision as sum_plan_employees,
sum(c.participants)::double precision as sum_plan_participants,
sum(c.premium)::double precision as sum_plan_premium,
/* Precalculated platform values */
concat('http://localhost:3000', replace('/agents/[npn]', '[npn]', c."npn"::text)) as "benefeature-link",
(count(*) over ())::integer as total_count
from
(
select cc.*
from (select
/* Agent attribute columns */
ag.npn,
ag."agent-name",
ag.discoverycontactid,
ag.discoverydataprofileurl,
ag."broker-master-name",
ag."broker-name-cleansed",
ag."broker-affiliation",
ag."broker-cb-domain",
ag."broker-parent-master-id",
ag."broker-association-master-id",
ag."broker-is-parent",
ag."broker-is-association",
ag."broker-general-agent-flag",
ag."broker-master-office-id",
ag."office-address-line-1",
ag."office-address-line-2",
ag."office-address-city",
ag."office-address-state",
ag."office-address-zip",
ag."office-address-county",
ag."office-latitude",
ag."office-longitude",
ag."office-geometry-point",
ag."prepared-email-business-type",
ag."modeled-email-flag",
ag.primary_phone,
ag."eligible-for-qualification",
ag."linkedin-urn",
ag."linkedin-url",
ag."linkedin-refresh-date",
ag."linkedin-location-city",
ag."linkedin-location-state",
ag."linkedin-location-top-zip",
ag."linkedin-location-longitude",
ag."linkedin-location-latitude",
ag."linkedin-about",
ag."linkedin-website",
ag."linkedin-headline",
ag."linkedin-followers",
ag."linkedin-full_name",
ag."linkedin-connections",
ag."linkedin-experience-affiliation",
ag."linkedin-experience-company",
ag."linkedin-current-title",
ag."linkedin-experience-location",
ag."linkedin-experience-start",
ag."linkedin-experience-end",
ag."linkedin-started-with-company-ago-band",
ag."linkedin-started-with-company-months-ago",
ag."linkedin-title-started-ago-band",
ag."linkedin-title-started-months-ago",
ag."prior-role-title",
ag."prior-role-company",
ag."prior-role-logo_url",
ag."prior-role-starts_at",
ag."prior-role-ends_at",
ag."prior-role-broker-master-id",
ag."prior-role-broker-master-name",
ag."prior-role-carrier-master-id",
ag."prior-role-carrier-master-name",
ag."prior-role-months_with_company",
ag."prior-role-months_in_role",
/* Windowed aggregates for some market-level filters */
/* All carriers
Do not bother with selecting distinct values here, outer will re-aggregate carriers appropriately */
array_agg(cc_inner."carrier-name")
over (partition by ag."npn") as all_carriers,
/* All core columns */
cc_inner.*
from insurance_plans_core_brokers as cc_inner
cross join lateral (select y as agent
from jsonb_array_elements(cc_inner."qualified_agents") as y
where y is not null) as cj_ag
join agents as ag
on ag.npn = (cj_ag.agent ->> 'npn')::integer
) as cc
where 1 = 1 /* Placeholder for potential empty dynamic filtering */
/* Apply broker IDs if defined */
/* Dynamic conditions */
/* Static filtering */
/* Filter out any agents which are considered to be "minor" */
and "is-minor-broker-flag" = 0
/* Checks whether the name is populated */
and length("agent-name") > 0
/* Territory filtering */
) as "c"
/* For grouping, don't do rollups, they're very slow
Just run the query on each basis if other perspectives are required */
group by
/* Core values to pull for the agent results */
c."npn",
c."agent-name",
/* Broker */
c.broker,
c."broker-master-id",
c.parent,
c."parent-master-id",
c.association,
c."association-master-id",
c."broker-cb-domain",
c."broker-general-agent-flag",
c."is-minor-broker-flag",
c."broker-master-office-id",
c."office-name",
c."office-address-line-1",
c."office-address-line-2",
c."office-address-city",
c."office-address-state",
c."office-address-zip",
c."office-address-county",
c."office-latitude",
c."office-longitude",
c."prepared-email-business-type",
c."modeled-email-flag",
c."primary_phone",
c."eligible-for-qualification",
c."linkedin-urn",
c."linkedin-url",
c."linkedin-refresh-date",
c."linkedin-location-city",
c."linkedin-location-state",
c."linkedin-location-top-zip",
c."linkedin-location-longitude",
c."linkedin-location-latitude",
c."linkedin-about",
c."linkedin-website",
c."linkedin-headline",
c."linkedin-followers",
c."linkedin-full_name",
c."linkedin-connections",
c."linkedin-experience-affiliation",
c."linkedin-experience-company",
c."linkedin-current-title",
c."linkedin-experience-location",
c."linkedin-experience-start",
c."linkedin-experience-end",
c."linkedin-started-with-company-months-ago",
c."linkedin-title-started-months-ago"
order by
/* Order by clause */
/* Default sort first on whether the agent was enriched successfully */
case when c."linkedin-current-title" is not null then 0 else 1 end,
/* Default on total plans next */
"count_plans" desc nulls last,
c."linkedin-full_name",
c."agent-name",
c."npn"
/* Offset and limit clauses */
offset 0 limit 25;