-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhierarchical comps.sql
329 lines (307 loc) · 11.2 KB
/
hierarchical comps.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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
--This report creates multi-layered hierarchical comps at both the dept level, grp dept level, and store level.
--create comp sales for this year at the prod_dept level
create temp table sales_ty as
select
pd.PROD_DEPT_ID,
pd.PROD_DEPT_DESCR,
a11.STORE_ID,
sum(a11.NET_SALES_AMT + a11.COUP_AMT) Revenue_ty,
count(distinct a11.ITEM_KEY ) ttls_sold_ty,
sum(a11.NET_SALES_AMT+ a11.COUP_AMT - a11.NET_COST) margin_dlr_ty
from SALES_FACT_DTL a11
left join REL_IS_GOSHP_ALL a12
on (a11.IS_GOSHP_ID = a12.IS_GOSHP_ID)
left join DBA1.LU_COMP_STORE a13
on (a11.STORE_ID = a13.STORE_ID)
left join REL_STORE_GRP_ALL a14
on (a11.STORE_ID = a14.STORE_ID)
left join LU_ITEM a15
on (a11.ITEM_KEY = a15.ITEM_KEY)
left join lu_prod_dept pd
on(pd.PROD_DEPT_ID= a15.PROD_DEPT_ID and pd.GRP_DEPT_ID= a15.GRP_DEPT_ID)
left join lu_grp_dept gd
on(gd.GRP_DEPT_ID = a15.GRP_DEPT_ID)
left join lu_date dd
on(a11.BUS_DATE=dd.date)
left join lu_prod_code pc
on(pc.PROD_CODE = a15.PROD_CODE)
left join dw_stage..PROD_CAT1_stg2 pp1
on(pp1.PROD_CAT1_KEY= a15.PROD_CAT1_KEY )
left join dw_stage..PROD_CAT2_stg2 pp2
on(pp2.PROD_CAT2_KEY= a15.PROD_CAT2_KEY )
left join dw_stage..PROD_CAT3_STG2 pp3
on(pp3.PROD_CAT3_KEY =a15.PROD_CAT3_KEY)
where (a15.GRP_DEPT_ID not in (-2, -1, 13)
and a11.IS_OVRNG in ('N')
and a14.STORE_GRP_ALL_ID in (1000)
and a11.BUS_DATE >= a13.COMP_DATE
and a13.COMP_FLG in ('TY')
and a12.IS_GOSHP_ALL_ID=40
and comp_type in(0)
--and a11.STORE_ID=9604
--and a15.PROD_DEPT_ID not in(116,89,113)
and a11.STORE_ID not between 9303 and 9347
and a11.BUS_DATE between To_Date('1/01/2015', 'mm/dd/yyyy') and To_Date('12/31/2015', 'mm/dd/yyyy')
and (a13.CLOSE_DATE >= current_date-1
or a13.CLOSE_DATE is null))
group by 1,2,3
;
--create comp sales ly at the prod dept level
create temp table sales_ly as
select
pd.PROD_DEPT_ID,
pd.PROD_DEPT_DESCR,
a11.STORE_ID,
sum(a11.NET_SALES_AMT + a11.COUP_AMT) Revenue_ly,
count(distinct a11.ITEM_KEY ) ttls_sold_ly,
sum(a11.NET_SALES_AMT+ a11.COUP_AMT - a11.NET_COST) margin_dlr_ly
from SALES_FACT_DTL a11
left join REL_IS_GOSHP_ALL a12
on (a11.IS_GOSHP_ID = a12.IS_GOSHP_ID)
left join DBA1.LU_COMP_STORE a13
on (a11.STORE_ID = a13.STORE_ID)
left join REL_STORE_GRP_ALL a14
on (a11.STORE_ID = a14.STORE_ID)
left join LU_ITEM a15
on (a11.ITEM_KEY = a15.ITEM_KEY)
left join lu_prod_dept pd
on(pd.PROD_DEPT_ID= a15.PROD_DEPT_ID and pd.GRP_DEPT_ID= a15.GRP_DEPT_ID)
left join lu_grp_dept gd
on(gd.GRP_DEPT_ID = a15.GRP_DEPT_ID)
left join lu_date dd
on(a11.BUS_DATE=dd.date)
left join lu_prod_code pc
on(pc.PROD_CODE = a15.PROD_CODE)
left join dw_stage..PROD_CAT1_stg2 pp1
on(pp1.PROD_CAT1_KEY= a15.PROD_CAT1_KEY )
left join dw_stage..PROD_CAT2_stg2 pp2
on(pp2.PROD_CAT2_KEY= a15.PROD_CAT2_KEY )
left join dw_stage..PROD_CAT3_STG2 pp3
on(pp3.PROD_CAT3_KEY =a15.PROD_CAT3_KEY)
where (a15.GRP_DEPT_ID not in (-2, -1, 13)
and a11.IS_OVRNG in ('N')
and a14.STORE_GRP_ALL_ID in (1000)
and a11.BUS_DATE >= a13.COMP_DATE
and a13.COMP_FLG in ('LY')
and a12.IS_GOSHP_ALL_ID=40
and comp_type in(0)
--and a11.STORE_ID=9604
and a11.STORE_ID not between 9303 and 9347
and a11.BUS_DATE between To_Date('1/01/2014', 'mm/dd/yyyy') and To_Date('12/31/2014', 'mm/dd/yyyy')
and (a13.CLOSE_DATE >= current_date-1
or a13.CLOSE_DATE is null))
group by 1,2,3
;
--find all prod depts in either year
create temp table prod_dept_union as
select PROD_DEPT_ID,
PROD_DEPT_DESCR, store_id
from sales_ty t
group by 1,2,3
union
select PROD_DEPT_ID,
PROD_DEPT_DESCR, store_id
from sales_ly t
group by 1,2,3
;
create temp table total_sales_ty as
select store_id, sum(revenue_ty) revenue_tyt
from sales_ty
group by 1;
create temp table total_sales_ly as
select store_id, sum(revenue_ly) revenue_lyt
from sales_ly
group by 1;
--Group prod dept data together
create temp table t_comp as
select p.PROD_DEPT_ID,
p.PROD_DEPT_DESCR,
p.STORE_ID,
pd.grp_dept_id,
nvl(Revenue_ty,0) revenue_ty,
nvl(Revenue_ly,0) revenue_ly,
case when revenue_ly =0 then 0 when (revenue_ly is null or revenue_ly=0) then 0 when ((nvl(revenue_ty,revenue_ly) -nvl(revenue_ly , revenue_ty))/ nvl(revenue_ly , revenue_ty))=0 then 0
else ((nvl(revenue_ty,revenue_ly) -nvl(revenue_ly , revenue_ty))/ nvl(revenue_ly , revenue_ty)) end sales_comp,
nvl(Revenue_ty,0) / ttty.revenue_tyt pct_to_total_ty,
nvl(Revenue_ly,0) / ttly.revenue_lyt pct_to_total_ly,
nvl(ttls_sold_ty,0) ttls_sold_ty,
nvl(ttls_sold_ly,0) ttls_sold_ly,
nvl(margin_dlr_ty,0) margin_dlr_ty,
nvl(margin_dlr_ly,0) margin_dlr_ly
from prod_dept_union p
left join sales_ty ty
on(ty.prod_dept_id=p.prod_dept_id and ty.store_id=p.store_id)
left join sales_ly ly
on(ly.prod_dept_id=p.prod_dept_id and ly.store_id=p.store_id)
left join lu_prod_Dept pd
on(pd.prod_dept_id=p.prod_dept_id)
left join total_sales_ty ttty
on(ttty.store_id=p.store_id)
left join total_sales_ly ttly
on(ttly.store_id=p.store_id)
;
------start grp dept here
--create comp sales for this year at the grp_dept level
create temp table sales_ty_g as
select
gd.GRP_DEPT_ID,
gd.GRP_DEPT_DESCR,
a11.STORE_ID,
sum(a11.NET_SALES_AMT + a11.COUP_AMT) Revenue_ty,
count(distinct a11.ITEM_KEY ) ttls_sold_ty,
sum(a11.NET_SALES_AMT+ a11.COUP_AMT - a11.NET_COST) margin_dlr_ty
from SALES_FACT_DTL a11
left join REL_IS_GOSHP_ALL a12
on (a11.IS_GOSHP_ID = a12.IS_GOSHP_ID)
left join DBA1.LU_COMP_STORE a13
on (a11.STORE_ID = a13.STORE_ID)
left join REL_STORE_GRP_ALL a14
on (a11.STORE_ID = a14.STORE_ID)
left join LU_ITEM a15
on (a11.ITEM_KEY = a15.ITEM_KEY)
left join lu_prod_dept pd
on(pd.PROD_DEPT_ID= a15.PROD_DEPT_ID and pd.GRP_DEPT_ID= a15.GRP_DEPT_ID)
left join lu_grp_dept gd
on(gd.GRP_DEPT_ID = a15.GRP_DEPT_ID)
left join lu_date dd
on(a11.BUS_DATE=dd.date)
left join lu_prod_code pc
on(pc.PROD_CODE = a15.PROD_CODE)
left join dw_stage..PROD_CAT1_stg2 pp1
on(pp1.PROD_CAT1_KEY= a15.PROD_CAT1_KEY )
left join dw_stage..PROD_CAT2_stg2 pp2
on(pp2.PROD_CAT2_KEY= a15.PROD_CAT2_KEY )
left join dw_stage..PROD_CAT3_STG2 pp3
on(pp3.PROD_CAT3_KEY =a15.PROD_CAT3_KEY)
where (a15.GRP_DEPT_ID not in (-2, -1, 13)
and a11.IS_OVRNG in ('N')
and a14.STORE_GRP_ALL_ID in (1000)
and a11.BUS_DATE >= a13.COMP_DATE
and a13.COMP_FLG in ('TY')
and a12.IS_GOSHP_ALL_ID=40
and comp_type in(0)
--and a11.STORE_ID=9604
--and a15.PROD_DEPT_ID not in(116,89,113)
and a11.STORE_ID not between 9303 and 9347
and a11.BUS_DATE between To_Date('1/01/2015', 'mm/dd/yyyy') and To_Date('12/31/2015', 'mm/dd/yyyy')
and (a13.CLOSE_DATE >= current_date-1
or a13.CLOSE_DATE is null))
group by 1,2,3
;
--create comp sales ly at the prod dept level
create temp table sales_ly_G as
select
gd.GRP_DEPT_ID,
gd.GRP_DEPT_DESCR,
a11.STORE_ID,
sum(a11.NET_SALES_AMT + a11.COUP_AMT) Revenue_ly,
count(distinct a11.ITEM_KEY ) ttls_sold_ly,
sum(a11.NET_SALES_AMT+ a11.COUP_AMT - a11.NET_COST) margin_dlr_ly
from SALES_FACT_DTL a11
left join REL_IS_GOSHP_ALL a12
on (a11.IS_GOSHP_ID = a12.IS_GOSHP_ID)
left join DBA1.LU_COMP_STORE a13
on (a11.STORE_ID = a13.STORE_ID)
left join REL_STORE_GRP_ALL a14
on (a11.STORE_ID = a14.STORE_ID)
left join LU_ITEM a15
on (a11.ITEM_KEY = a15.ITEM_KEY)
left join lu_prod_dept pd
on(pd.PROD_DEPT_ID= a15.PROD_DEPT_ID and pd.GRP_DEPT_ID= a15.GRP_DEPT_ID)
left join lu_grp_dept gd
on(gd.GRP_DEPT_ID = a15.GRP_DEPT_ID)
left join lu_date dd
on(a11.BUS_DATE=dd.date)
left join lu_prod_code pc
on(pc.PROD_CODE = a15.PROD_CODE)
left join dw_stage..PROD_CAT1_stg2 pp1
on(pp1.PROD_CAT1_KEY= a15.PROD_CAT1_KEY )
left join dw_stage..PROD_CAT2_stg2 pp2
on(pp2.PROD_CAT2_KEY= a15.PROD_CAT2_KEY )
left join dw_stage..PROD_CAT3_STG2 pp3
on(pp3.PROD_CAT3_KEY =a15.PROD_CAT3_KEY)
where (a15.GRP_DEPT_ID not in (-2, -1, 13)
and a11.IS_OVRNG in ('N')
and a14.STORE_GRP_ALL_ID in (1000)
and a11.BUS_DATE >= a13.COMP_DATE
and a13.COMP_FLG in ('LY')
and a12.IS_GOSHP_ALL_ID=40
and comp_type in(0)
--and a11.STORE_ID=9604
and a11.STORE_ID not between 9303 and 9347
and a11.BUS_DATE between To_Date('1/01/2014', 'mm/dd/yyyy') and To_Date('12/31/2014', 'mm/dd/yyyy')
and (a13.CLOSE_DATE >= current_date-1
or a13.CLOSE_DATE is null))
group by 1,2,3
;
--find all prod depts in either year
create temp table grp_dept_union as
select GRP_DEPT_ID,
GRP_DEPT_DESCR, store_id
from sales_ty_g t
group by 1,2,3
union
select GRP_DEPT_ID,
GRP_DEPT_DESCR, store_id
from sales_ly_g t
group by 1,2,3
;
insert into t_comp
select null placeholder,
p.GRP_DEPT_DESCR,
p.STORE_ID,
p.grp_dept_id,
nvl(Revenue_ty,0) revenue_ty,
nvl(Revenue_ly,0) revenue_ly,
case when revenue_ly =0 then 0 when (revenue_ly is null or revenue_ly=0) then 0 when ((nvl(revenue_ty,revenue_ly) -nvl(revenue_ly , revenue_ty))/ nvl(revenue_ly , revenue_ty))=0 then 0
else ((nvl(revenue_ty,revenue_ly) -nvl(revenue_ly , revenue_ty))/ nvl(revenue_ly , revenue_ty)) end sales_comp,
nvl(Revenue_ty,0) / ttty.revenue_tyt pct_to_total_ty,
nvl(Revenue_ly,0) / ttly.revenue_lyt pct_to_total_ly,
nvl(ttls_sold_ty,0) ttls_sold_ty,
nvl(ttls_sold_ly,0) ttls_sold_ly,
nvl(margin_dlr_ty,0) margin_dlr_ty,
nvl(margin_dlr_ly,0) margin_dlr_ly
from grp_dept_union p
left join sales_ty_g ty
on(ty.grp_dept_id=p.grp_dept_id and ty.store_id=p.store_id)
left join sales_ly_g ly
on(ly.grp_dept_id=p.grp_dept_id and ly.store_id=p.store_id)
left join total_sales_ty ttty
on(ttty.store_id=p.store_id)
left join total_sales_ly ttly
on(ttly.store_id=p.store_id)
;
--get data for store total
create temp table grp_dept_final as
select null placeholder,
p.GRP_DEPT_DESCR,
p.STORE_ID,
p.grp_dept_id,
nvl(Revenue_ty,0) revenue_ty,
nvl(Revenue_ly,0) revenue_ly,
case when revenue_ly =0 then 0 when (revenue_ly is null or revenue_ly=0) then 0 when ((nvl(revenue_ty,revenue_ly) -nvl(revenue_ly , revenue_ty))/ nvl(revenue_ly , revenue_ty))=0 then 0
else ((nvl(revenue_ty,revenue_ly) -nvl(revenue_ly , revenue_ty))/ nvl(revenue_ly , revenue_ty)) end sales_comp,
nvl(Revenue_ty,0) / (select sum(revenue_ty) from sales_ty) pct_to_total_ty,
nvl(Revenue_ly,0) / (select sum(revenue_ly) from sales_ly) pct_to_total_ly,
nvl(ttls_sold_ty,0) ttls_sold_ty,
nvl(ttls_sold_ly,0) ttls_sold_ly,
nvl(margin_dlr_ty,0) margin_dlr_ty,
nvl(margin_dlr_ly,0) margin_dlr_ly
from grp_dept_union p
left join sales_ty_g ty
on(ty.grp_dept_id=p.grp_dept_id and ty.store_id=p.store_id)
left join sales_ly_g ly
on(ly.grp_dept_id=p.grp_dept_id and ly.store_id=p.store_id)
;
--get grp_dept_total
--select *
--from grp_Dept_final;
--get store total
insert into t_comp
select placeholder, 'Store Total' as grp_dept_descr, store_id, 22 as grp_dept_id, sum(revenue_ty) revenue_ty,
sum(revenue_ly) revenue_ly, (sum(revenue_ty) - sum(revenue_ly)) /sum(revenue_ly) sales_comp,1 pct_to_total_ty,1 pct_to_total_ly, sum(ttls_sold_ty) ttls_sold_ty, sum(ttls_sold_ly) ttls_sold_ly, sum(margin_dlr_ty) margin_dlr_ty, sum(margin_dlr_ly) margin_dlr_ly
from grp_dept_final
group by 1,2,3,4;
select store_id, prod_dept_id, prod_dept_descr, grp_dept_id, revenue_ty, revenue_ly, sales_comp, pct_to_total_ty, pct_to_total_ly, ttls_sold_ty, ttls_sold_ly, margin_dlr_ty, margin_dlr_ly
from t_comp
order by 1,4,5