Repository navigation
Expand file tree
/
Copy pathАнализ кассиров_proc.sql
More file actions
434 lines (392 loc) · 41.8 KB
/
Copy pathАнализ кассиров_proc.sql
File metadata and controls
434 lines (392 loc) · 41.8 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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
USE [FrontStore]
GO
/****** Object: StoredProcedure [dbo].[KassirForecast] Script Date: 07/14/2015 10:04:18 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[KassirForecast]
(
@CurrentWeek date = null,
@K decimal = null
)
AS
set nocount on
BEGIN
if @CurrentWeek is null Set @CurrentWeek = convert(date,getdate())
declare @NextWeek date = dateadd(d,7,@CurrentWeek)
declare @PreviousWeek date = dateadd(d,-7,@CurrentWeek)
set datefirst 1
IF OBJECT_ID('tempdb..#Shops') IS NOT NULL BEGIN DROP TABLE #Shops Print 'Deleted #Shops' END
select Shopid,ShopName,[ParentShopTypeID] ,[ParentShopTypeName], (case when ParentShopTypeID!=103 then 1 else 0 end) as SM
into #Shops
from Backstore.dbo.EntShops
where TradeArea is not null
and [Active]=1
and [SQLServer] is not null
/*Установка коэффициентов*/
--Время на операции сканирования одной SKU, сек
declare @SMcoef decimal(8,3) = 3.5
declare @GMcoef decimal(8,3) = 4
--Время на нерассчитываемые операции на один чек (открытие + закрытие), сек
declare @SMbill decimal(8,3) = 25
declare @GMbill decimal(8,3) = 30
--К-т использования времен
if @K is null Set @K = 0.7
/*Супера + Гипера*/
--1.1. Продажи на прошлой неделе
IF OBJECT_ID('tempdb..#tab1') IS NOT NULL BEGIN DROP TABLE #tab1 Print 'Deleted #tab1' END
select
BillShifts.ShopId
,convert(date,BillShifts.DateOpen) as Day
,DATEPART(dw,BillShifts.DateOpen) as WeekNum
,datepart(hour,BillHeaders.DateOpen) as Hour
,count(distinct BillShifts.LogicalNumber) as Casses
,count(distinct BillHeaders.PersonId) as Persons
,count(*) as BillCount
,sum(lines) as Positions
,(sum(lines)*(case when #Shops.SM=1 then @SMcoef else @GMcoef end) + (case when #Shops.SM=1 then @SMbill else @GMbill end) * count(*) ) PositionsTime
,(sum(lines)*(case when #Shops.SM=1 then @SMcoef else @GMcoef end) + (case when #Shops.SM=1 then @SMbill else @GMbill end) * count(*) )/cast(60*60*@K as decimal(18,9)) as N
into #tab1
from dbo.BillShifts (nolock)
join dbo.BillHeaders (nolock) on BillShifts.Shiftid=BillHeaders.Shiftid
join #Shops on #Shops.ShopID=BillShifts.ShopId --and #Shops.SM=1
where DATEPART(wk, @PreviousWeek) = DATEPART(wk, BillShifts.DateOpen)
and DATEPART(year, @PreviousWeek) = DATEPART(year, BillShifts.DateOpen)
--and BillShifts.ShopId=16
group by datepart(hour,BillHeaders.DateOpen), DATEPART(dw,BillShifts.DateOpen)
,convert(date,BillShifts.DateOpen),BillShifts.ShopId, #Shops.SM
order by DATEPART(dw,BillShifts.DateOpen), datepart(hour,BillHeaders.DateOpen)
--1.2. Расчет распределения продаж по часам в каждый день недели.
IF OBJECT_ID('tempdb..#SM_CurrYearPrevWeek') IS NOT NULL BEGIN DROP TABLE #SM_CurrYearPrevWeek Print 'Deleted #SM_CurrYearPrevWeek' END
select z.shopid, Hour,z.WeekNum, Z.Day, H.IsHolidayDay, H.IsNonworkingDay, H.HolidayTypeId
,Casses,Persons,BillCount
,(case when q.BillCountSum = 0 then null else z.BillCount*1.0/q.BillCountSum end) as Ratio
,(case when p.BillCountSumWeek = 0 then null else q.BillCountSum*1.0/p.BillCountSumWeek end) as RatioWeek
,Positions
,isnull(N,1) as RawN
,N-N%1+case when N%1>0.1 or N<=0.1 then 1 else 0 end as N
,(case when N=0 then null else PositionsTime/(N-N%1+case when N%1>0.1 or N<=0.1 then 1 else 0 end) end) as FullTime
,(case when N=0 then null else 3600-PositionsTime/(N-N%1+case when N%1>0.1 or N<=0.1 then 1 else 0 end) end) as OutTime
,1 as Today
into #SM_CurrYearPrevWeek
from #tab1 z
left join
(
select shopid, weeknum,sum(BillCount) as BillCountSum
from #tab1 z
group by shopid,weeknum
) q on q.WeekNum=z.WeekNum and q.shopid=z.ShopId
left join
(
select shopid, sum(BillCount) as BillCountSumWeek
from #tab1 z group by shopid
) p on z.WeekNum>0 and p.shopid=z.ShopId
left join [BackStore].[dbo].[Holidays] H on H.Date=Z.Day
order by z.shopid, WeekNum,Hour
--2.1. Продажи на этой неделе (чисто для проверки прогнозов, в расчете не участвует)
IF OBJECT_ID('tempdb..#tab4') IS NOT NULL BEGIN DROP TABLE #tab4 Print 'Deleted #tab4' END
select
BillShifts.ShopId
,convert(date,BillShifts.DateOpen) as Day
,DATEPART(dw,BillShifts.DateOpen) as WeekNum
,datepart(hour,BillHeaders.DateOpen) as Hour
,count(distinct BillShifts.LogicalNumber) as Casses
,count(distinct BillHeaders.PersonId) as Persons
,count(*) as BillCount
,sum(lines) as Positions
,(sum(lines)*(case when #Shops.SM=1 then @SMcoef else @GMcoef end) + (case when #Shops.SM=1 then @SMbill else @GMbill end) * count(*) ) PositionsTime
,(sum(lines)*(case when #Shops.SM=1 then @SMcoef else @GMcoef end) + (case when #Shops.SM=1 then @SMbill else @GMbill end) * count(*) )/cast(60*60*@K as decimal(18,9)) as N
into #tab4
from dbo.BillShifts (nolock)
join dbo.BillHeaders (nolock) on BillShifts.Shiftid=BillHeaders.Shiftid
join #Shops on #Shops.ShopID=BillShifts.ShopId --and #Shops.SM=1
where DATEPART(wk, @CurrentWeek) = DATEPART(wk, BillShifts.DateOpen)
and DATEPART(year, @CurrentWeek) = DATEPART(year, BillShifts.DateOpen)
--and BillShifts.ShopId=16
group by datepart(hour,BillHeaders.DateOpen), DATEPART(dw,BillShifts.DateOpen)
,convert(date,BillShifts.DateOpen), BillShifts.ShopId, #Shops.SM
order by DATEPART(dw,BillShifts.DateOpen), datepart(hour,BillHeaders.DateOpen)
--2.2. Расчет распределения продаж по часам в каждый день недели.
IF OBJECT_ID('tempdb..#SM_CurrYearCurrWeek') IS NOT NULL BEGIN DROP TABLE #SM_CurrYearCurrWeek Print 'Deleted #SM_CurrYearCurrWeek' END
select z.ShopId, Hour,z.WeekNum,Casses,Persons,BillCount, Z.Day, H.IsHolidayDay, H.IsNonworkingDay, H.HolidayTypeId
,(case when q.BillCountSum = 0 then null else z.BillCount*1.0/q.BillCountSum end) as Ratio
,(case when p.BillCountSumWeek = 0 then null else q.BillCountSum*1.0/p.BillCountSumWeek end) as RatioWeek
,Positions
,isnull(N,1) as RawN
,N-N%1+case when N%1>0.1 or N<=0.1 then 1 else 0 end as N
,(case when N=0 then null else PositionsTime/(N-N%1+case when N%1>0.1 or N<=0.1 then 1 else 0 end) end) as FullTime
,(case when N=0 then null else 3600-PositionsTime/(N-N%1+case when N%1>0.1 or N<=0.1 then 1 else 0 end) end) as OutTime
,1 as Today
into #SM_CurrYearCurrWeek
from #tab4 z
left join
(
select shopid, weeknum,sum(BillCount) as BillCountSum
from #tab4 z
group by shopid, weeknum
) q
on q.WeekNum=z.WeekNum and q.shopid=z.ShopId
left join
(
select shopid, sum(BillCount) as BillCountSumWeek
from #tab1 z group by shopid
) p on z.WeekNum>0 and p.shopid=z.ShopId
left join [BackStore].[dbo].[Holidays] H on H.Date=Z.Day
order by WeekNum,Hour
--3.1. Год назад - прошлая неделя
IF OBJECT_ID('tempdb..#tab2') IS NOT NULL BEGIN DROP TABLE #tab2 Print 'Deleted #tab2' END
select
BillShifts.ShopId
,convert(date,BillShifts.DateOpen) as Day
,DATEPART(dw,BillShifts.DateOpen) as WeekNum
,datepart(hour,BillHeaders.DateOpen) as Hour
,count(distinct BillShifts.LogicalNumber) as Casses
,count(distinct BillHeaders.PersonId) as Persons
,count(*) as BillCount
,sum(lines) as Positions
,(sum(lines)*(case when #Shops.SM=1 then @SMcoef else @GMcoef end) + (case when #Shops.SM=1 then @SMbill else @GMbill end) * count(*) ) PositionsTime
,(sum(lines)*(case when #Shops.SM=1 then @SMcoef else @GMcoef end) + (case when #Shops.SM=1 then @SMbill else @GMbill end) * count(*) )/cast(60*60*@K as decimal(18,9)) as N
into #tab2
from dbo.BillShifts (nolock)
join dbo.BillHeaders (nolock) on BillShifts.Shiftid=BillHeaders.Shiftid
join #Shops on #Shops.ShopID=BillShifts.ShopId --and #Shops.SM=1
where DATEPART(wk, BillShifts.DateOpen) = DATEPART(wk, @PreviousWeek)
and DATEPART(year, BillShifts.DateOpen)=DATEPART(year, dateadd(year,-1,@PreviousWeek))
--and BillShifts.ShopId=16
group by datepart(hour,BillHeaders.DateOpen), DATEPART(dw,BillShifts.DateOpen)
,convert(date,BillShifts.DateOpen), BillShifts.ShopId, #Shops.SM
order by DATEPART(dw,BillShifts.DateOpen), datepart(hour,BillHeaders.DateOpen)
--3.2. Расчет распределения продаж по часам в каждый день недели.
IF OBJECT_ID('tempdb..#Super_YearAgo_PrevWeek') IS NOT NULL BEGIN DROP TABLE #Super_YearAgo_PrevWeek Print 'Deleted #Super_YearAgo_PrevWeek' END
select z.shopid, Hour,z.WeekNum,Casses,Persons,BillCount, Z.Day, H.IsHolidayDay, H.IsNonworkingDay, H.HolidayTypeId
,(case when q.BillCountSum = 0 then null else z.BillCount*1.0/q.BillCountSum end) as Ratio
,(case when p.BillCountSumWeek = 0 then null else q.BillCountSum*1.0/p.BillCountSumWeek end) as RatioWeek
,Positions
,isnull(N,1) as RawN
,N-N%1+case when N%1>0.1 or N<=0.1 then 1 else 0 end as N
,(case when N=0 then null else PositionsTime/(N-N%1+case when N%1>0.1 or N<=0.1 then 1 else 0 end) end) as FullTime
,(case when N=0 then null else 3600-PositionsTime/(N-N%1+case when N%1>0.1 or N<=0.1 then 1 else 0 end) end) as OutTime
,0 as Today
into #Super_YearAgo_PrevWeek
from #tab2 z
left join
(
select shopid,weeknum,sum(BillCount) as BillCountSum
from #tab2 z
group by shopid,weeknum
) q on q.WeekNum=z.WeekNum and q.shopid=z.shopid
left join
(
select shopid,sum(BillCount) as BillCountSumWeek
from #tab2 z group by shopid
) p on z.WeekNum>0 and p.shopid=z.shopid
left join [BackStore].[dbo].[Holidays] H on H.Date=Z.Day
order by WeekNum,Hour
--4.1. Год назад - следуюшая неделя
IF OBJECT_ID('tempdb..#tab3') IS NOT NULL BEGIN DROP TABLE #tab3 Print 'Deleted #tab3' END
select
BillShifts.ShopId
,convert(date,BillShifts.DateOpen) as Day
,DATEPART(dw,BillShifts.DateOpen) as WeekNum
,datepart(hour,BillHeaders.DateOpen) as Hour
,count(distinct BillShifts.LogicalNumber) as Casses
,count(distinct BillHeaders.PersonId) as Persons
,count(*) as BillCount
,sum(lines) as Positions
,(sum(lines)*(case when #Shops.SM=1 then @SMcoef else @GMcoef end) + (case when #Shops.SM=1 then @SMbill else @GMbill end) * count(*) ) PositionsTime
,(sum(lines)*(case when #Shops.SM=1 then @SMcoef else @GMcoef end) + (case when #Shops.SM=1 then @SMbill else @GMbill end) * count(*) )/cast(60*60*@K as decimal(18,9)) as N
into #tab3
from dbo.BillShifts (nolock)
join dbo.BillHeaders (nolock) on BillShifts.Shiftid=BillHeaders.Shiftid
join #Shops on #Shops.ShopID=BillShifts.ShopId --and #Shops.SM=1
where DATEPART(wk, BillShifts.DateOpen) = DATEPART(wk, @NextWeek)
and DATEPART(year, BillShifts.DateOpen)=DATEPART(year, dateadd(year,-1,@NextWeek))
--and BillShifts.ShopId=16
group by datepart(hour,BillHeaders.DateOpen), DATEPART(dw,BillShifts.DateOpen)
,convert(date,BillShifts.DateOpen), BillShifts.ShopId, #Shops.SM
order by DATEPART(dw,BillShifts.DateOpen), datepart(hour,BillHeaders.DateOpen)
--4.2. асчет распределения продаж по часам в каждый день недели.
IF OBJECT_ID('tempdb..#Super_YearAgo_NextWeek') IS NOT NULL BEGIN DROP TABLE #Super_YearAgo_NextWeek Print 'Deleted #Super_YearAgo_NextWeek' END
select z.ShopId, Hour,z.WeekNum,Casses,Persons,BillCount, Z.Day, H.IsHolidayDay, H.IsNonworkingDay, H.HolidayTypeId
,(case when q.BillCountSum = 0 then null else z.BillCount*1.0/q.BillCountSum end) as Ratio
,(case when p.BillCountSumWeek = 0 then null else q.BillCountSum*1.0/p.BillCountSumWeek end) as RatioWeek
,Positions
,isnull(N,1) as RawN
,N-N%1+case when N%1>0.1 or N<=0.1 then 1 else 0 end as N
,(case when N=0 then null else PositionsTime/(N-N%1+case when N%1>0.1 or N<=0.1 then 1 else 0 end) end) as FullTime
,(case when N=0 then null else 3600-PositionsTime/(N-N%1+case when N%1>0.1 or N<=0.1 then 1 else 0 end) end) as OutTime
,0 as Today
into #Super_YearAgo_NextWeek
from #tab3 z
left join
(
select ShopId, weeknum,sum(BillCount) as BillCountSum
from #tab3 z
group by ShopId, weeknum
) q on q.WeekNum=z.WeekNum and q.ShopId=z.ShopId
left join
(
select ShopId, sum(BillCount) as BillCountSumWeek
from #tab3 z group by ShopId
) p on z.WeekNum>0 and p.ShopId=z.ShopId
left join [BackStore].[dbo].[Holidays] H on H.Date=Z.Day
order by WeekNum,Hour
--5. К-т праздников. Год назад, поиск по трем неделям: эта, следующая, предыдущая
IF OBJECT_ID('tempdb..#HolidayZ') IS NOT NULL BEGIN DROP TABLE #HolidayZ Print 'Deleted #HolidayZ' END
select BillShifts.ShopId
, #Shops.SM
,convert(date,BillShifts.DateOpen) as Day
,DATEPART(dw,BillShifts.DateOpen) as WeekNum
,datepart(hour,BillHeaders.DateOpen) as Hour
, H.HolidayTypeId
, H.IsHolidayDay
, H.IsNonworkingDay
,count(*) as BillCount
into #HolidayZ
from dbo.BillShifts (nolock)
join dbo.BillHeaders (nolock) on BillShifts.Shiftid=BillHeaders.Shiftid
join #Shops on #Shops.ShopID=BillShifts.ShopId --and #Shops.SM=1
left join [BackStore].[dbo].[Holidays] H on H.Date=convert(date,BillShifts.DateOpen)
where ( DATEPART(wk, BillShifts.DateOpen) = DATEPART(wk, @PreviousWeek)
or DATEPART(wk, BillShifts.DateOpen) = DATEPART(wk, dateadd(d,7,@PreviousWeek))
or DATEPART(wk, BillShifts.DateOpen) = DATEPART(wk, dateadd(d,-7,@PreviousWeek))
)
and DATEPART(year, BillShifts.DateOpen)=DATEPART(year, dateadd(year,-1,@PreviousWeek))
--and BillShifts.ShopId=16
group by BillShifts.ShopId, #Shops.SM, convert(date,BillShifts.DateOpen)
,DATEPART(dw,BillShifts.DateOpen), datepart(hour,BillHeaders.DateOpen)
, H.HolidayTypeId, H.IsHolidayDay, H.IsNonworkingDay
order by DATEPART(dw,BillShifts.DateOpen), datepart(hour,BillHeaders.DateOpen)
--Сам к-т для магазина
IF OBJECT_ID('tempdb..#HolydayCoef') IS NOT NULL BEGIN DROP TABLE #HolydayCoef Print 'Deleted #HolydayCoef' END
select shopid
,sum(BillCountNonHoliday) *1.0/SUM(case when IsHolidayDay=0 then 1 else 0 end) as AvgBillNonHoliday --Делим кол-во чеков на кол-во дней НЕ праздников за весь период = ср. кол-во чеок вне праздников
,SUM(BillCountHoliday) *1.0/SUM(case when IsHolidayDay=1 then 1 else 0 end) as AvgBillHoliday --ср кол-во чеков в день праздника
,isnull(( SUM(BillCountHoliday) *1.0/SUM(case when IsHolidayDay=1 then 1 else 0 end) ) / ( sum(BillCountNonHoliday) *1.0/SUM(case when IsHolidayDay=0 then 1 else 0 end) ),1) as HolidayCoef
into #HolydayCoef
from
(select ShopId, Day, WeekNum, IsHolidayDay
, (case when IsHolidayDay=0 then sum(BillCount) end) as BillCountNonHoliday
, (case when IsHolidayDay=1 then sum(BillCount) end) as BillCountHoliday
from #HolidayZ
group by ShopId, Day, WeekNum, IsHolidayDay
) t1
group by shopid
/*Выводная таблица*/
--declare @CurrentWeek date = convert(date,getdate())
--declare @NextWeek date = dateadd(d,7,@CurrentWeek)
--declare @PreviousWeek date = dateadd(d,-7,@CurrentWeek)
select t1.Shopid as [Код магазина], #Shops.ShopName as [Магазин]
, convert(date,dateadd(day,-14,DD.Date)) as [Дата базы]
, convert(date,DD.Date) as [Дата прогноза],DD.[DayOfWeekRu] as [День недели]
, t2.Hour as [time]
,(case when t2.Hour=0 then '00:00-00:59'
when t2.Hour=1 then '01:00-01:59'
when t2.Hour=2 then '02:00-02:59'
when t2.Hour=3 then '03:00-03:59'
when t2.Hour=4 then '04:00-04:59'
when t2.Hour=5 then '05:00-05:59'
when t2.Hour=6 then '06:00-06:59'
when t2.Hour=7 then '07:00-07:59'
when t2.Hour=8 then '08:00-08:59'
when t2.Hour=9 then '09:00-09:59'
when t2.Hour=10 then '10:00-10:59'
when t2.Hour=11 then '11:00-11:59'
when t2.Hour=12 then '12:00-12:59'
when t2.Hour=13 then '13:00-13:59'
when t2.Hour=14 then '14:00-14:59'
when t2.Hour=15 then '15:00-15:59'
when t2.Hour=16 then '16:00-16:59'
when t2.Hour=17 then '17:00-17:59'
when t2.Hour=18 then '18:00-18:59'
when t2.Hour=19 then '19:00-19:59'
when t2.Hour=20 then '20:00-20:59'
when t2.Hour=21 then '21:00-21:59'
when t2.Hour=22 then '22:00-22:59'
when t2.Hour=23 then '23:00-23:59'
end) as [Диапазон времени]
--,t2.RawN as RawN_CurrYearPrevWeek --Оптимальное дробное кол-во кассиров на прошлой неделе
--,convert(int,t2.N) as N_CurrYearPrevWeek_Optimum --Оптимальное целое кол-во кассиров на прошлой неделе
,t2.Persons as [Кассиров по факту на прошлой неделе] --Кол-во кассиров на прошлой неделе
,t2.Casses as [Работало касс на прошлой неделе]
,t2.BillCount as [Чеков на прошлой неделе] --Кол-во чеков на прошлой неделе
,isnull(t3.RatioWeek/t1.RatioWeek * (case when H.IsHolidayDay=1 then HC.HolidayCoef else 1 end),1) as [К-т прогноза] --К-т с учетом дней недели, но без учета часовой нагрузки. Плюс усчет праздников
--,(t3.Ratio*t3.RatioWeek)/(t1.Ratio*t1.RatioWeek) as [К-т прогноза] --К-т с учетом дней недели и часовой нагрузки
,convert(int, isnull(( (t2.RawN* isnull(t3.RatioWeek/t1.RatioWeek * (case when H.IsHolidayDay=1 then HC.HolidayCoef else 1 end),1) )
-(t2.RawN*isnull(t3.RatioWeek/t1.RatioWeek * (case when H.IsHolidayDay=1 then HC.HolidayCoef else 1 end),1) )%1
+case when (t2.RawN*isnull(t3.RatioWeek/t1.RatioWeek * (case when H.IsHolidayDay=1 then HC.HolidayCoef else 1 end),1))%1>0.1
or (t2.RawN*isnull(t3.RatioWeek/t1.RatioWeek * (case when H.IsHolidayDay=1 then HC.HolidayCoef else 1 end),1))<0.1
then 1 else 0 end)
,t2.Persons*isnull(t3.RatioWeek/t1.RatioWeek * (case when H.IsHolidayDay=1 then HC.HolidayCoef else 1 end),1) ))
as [Прогноз кассиров] --Прогноз кол-ва кассиров с учетом часовой нагрузки И дней недели
--Сначала берется оптимальное число кассиров (дробное) и усножается на к-т прогноза. Если этого числа нет (т.е. нет данных прошлой недели), то берется фактическое число кассиров и умножается на к-т
--,convert(int, ( (t2.RawN*(t3.Ratio*t3.RatioWeek)/(t1.Ratio*t1.RatioWeek))
-- -(t2.RawN*(t3.Ratio*t3.RatioWeek)/(t1.Ratio*t1.RatioWeek))%1
-- +case when (t2.RawN*(t3.Ratio*t3.RatioWeek)/(t1.Ratio*t1.RatioWeek))%1>0.1 or (t2.RawN*(t3.Ratio*t3.RatioWeek)/(t1.Ratio*t1.RatioWeek))<0.1
-- then 1 else 0 end))
-- as [Прогноз кассиров] --Прогноз кол-ва кассиров с учетом часовой нагрузки И дней недели
--,convert(int,(t3.Ratio*t3.RatioWeek)/(t1.Ratio*t1.RatioWeek)*t2.BillCount)
-- as [BillCount_ForecastCurrentWeek] --Прогноз кол-ва чеков с учетом часовой нагрузки И дней недели
--,CYCW.BillCount as BillCount_FactCurrentWeek --кол-во счетов по факту (если берется @CurrentWeek с прошлой недели)
--, (t3.Ratio)/t1.Ratio) as Coef --К-т с учетом часовой нагрузки но без учета дней недели
--, t1.Ratio/t3.Ratio * t2.RawN as RawN_ft --Прогноз дробного кол-ва кассиров с учетом часовой нагрузки, но без учета дней недели
--,convert(int, ( (t2.RawN*t1.Ratio/t3.Ratio)-(t2.RawN*t1.Ratio/t3.Ratio)%1
-- + case when (t2.RawN*t1.Ratio/t3.Ratio)%1>0.1 or (t2.RawN*t1.Ratio/t3.Ratio)<0.1
-- then 1 else 0 end))
-- as N_ft --Прогноз целого кол-ва кассиров с учетом часовой нагрузки, но без учета дней недели
--,convert(int,t1.Ratio/t3.Ratio*t2.BillCount) as BillCount_CWft --Прогноз кол-ва чеков с учетом часовой нагрузки, но без учета дней недели
from #Super_YearAgo_PrevWeek t1
full outer join #SM_CurrYearPrevWeek t2 on t2.WeekNum=t1.WeekNum and t2.Hour=t1.Hour and t2.ShopId=t1.ShopId
full outer join #Super_YearAgo_NextWeek t3 on t3.WeekNum=t1.WeekNum and t3.Hour=t1.Hour and t3.ShopId=t1.ShopId
left join #SM_CurrYearCurrWeek CYCW on CYCW.WeekNum=t1.WeekNum and CYCW.Hour=t1.Hour and CYCW.ShopId=t1.ShopId
left join [BackStore].[dbo].[DateDimension] DD on DATEPART(wk, DD.Date)=DATEPART(wk, @NextWeek) and DATEPART(dw,DD.Date)=t2.WeekNum and DATEPART(year,DD.Date)=DATEPART(year, @NextWeek)
left join [BackStore].[dbo].[Holidays] H on H.Date=convert(date,DD.Date)
left join #HolydayCoef HC on HC.ShopId=t1.ShopId
join #Shops on #Shops.Shopid=t1.ShopId
where t1.ShopId is not null and t2.Hour is not null
order by t1.Shopid,t2.WeekNum, t2.hour
--Исходный код Алексея Алтунина
/*
select Hour,Casses,Persons,BillCount,Positions
,N-N%1+case when N%1>0.1 or N<0.1 then 1 else 0 end as N
,PositionsTime/(N-N%1+case when N%1>0.1 or N<0.1 then 1 else 0 end) as FullTime
,3600-PositionsTime/(N-N%1+case when N%1>0.1 or N<0.1 then 1 else 0 end) as OutTime
from
(
select
datepart(hour,BillHeaders.DateOpen) as Hour
,count(distinct BillShifts.LogicalNumber) as Casses
,count(distinct BillHeaders.PersonId) as Persons
,count(*) as BillCount
,sum(lines) as Positions
,(sum(lines)*3.5+25*count(*)) PositionsTime
,(sum(lines)*3.5+25*count(*))/cast(60*60*0.7 as decimal(18,9)) as N
from dbo.BillShifts (nolock)
join dbo.BillHeaders (nolock) on BillShifts.Shiftid=BillHeaders.Shiftid
where cast(BillShifts.DateOpen as date) between '2015.04.19' and '2015.04.19'
and BillShifts.ShopId=16
group by datepart(hour,BillHeaders.DateOpen)
) z
order by Hour
select Hour,Casses,Persons,BillCount,Positions,N-N%1+case when N%1>0.1 or N<0.1 then 1 else 0 end,PositionsTime/(N-N%1+case when N%1>0.1 or N<0.1 then 1 else 0 end)
,3600-PositionsTime/(N-N%1+case when N%1>0.1 or N<0.1 then 1 else 0 end)
from
(
select
datepart(hour,BillHeaders.DateOpen) as Hour
,count(distinct BillShifts.LogicalNumber) as Casses
,count(distinct BillHeaders.PersonId) as Persons
,count(*) as BillCount
,sum(lines) as Positions
,(sum(lines)*4+30*count(*)) PositionsTime
,(sum(lines)*4+30*count(*))/cast(60*60*0.7 as decimal(18,9)) as N
from dbo.BillShifts (nolock)
join dbo.BillHeaders (nolock) on BillShifts.Shiftid=BillHeaders.Shiftid
where cast(BillShifts.DateOpen as date) between '2015.04.19' and '2015.04.19'
and BillShifts.ShopId=263
group by datepart(hour,BillHeaders.DateOpen)
) z
order by Hour
*/
END