Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 25.95 KB | None | 0 0
  1. USE [FLConnect_Dbrf]
  2. GO
  3. /****** Object:  StoredProcedure [dbo].[Pp_CollectedDataByP]    Script Date: 10/16/2019 5:38:15 PM ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8.  
  9.  
  10. -- Updates:
  11. --Moran 3.11.2011: Exec Pp_Validation added                                    
  12. --Shmulik 23.11.2011: remove non users from Padj_Factor calculation
  13. --Shmulik:21.02.12: add EligibleHH<>6 to CouponFilterCode
  14. --Roni:28.03.12: add AccountCode as 3rd factor in order by
  15. --Shmulik:11.11.12: add Padj_FactorFromDeal
  16. --Shmulik 31.12.12: add Filter on Free Coupons
  17. --Sari 08.07.2013 -- Replace Tdbrf_DebriefParams with Tmng_DebriefParams
  18. --Moran 2013:   Insert R&D mode and Change production mode to use Tnam_Function table instead of temp table
  19. --              Use SP:Pnam_GetFilteredCoupons instead of internal calculation
  20. --              Take BC redemptions instead of Coupon red for Direct Mail channels in production mode
  21. --              Insert Update Tp_UpdatePadjFactorHistory table
  22. --              Use PrintDayPurchase parameter in production mode
  23. --              Update SP:Pp_ValidationByP
  24. --Shmulik 20.08.2014 Add BCusgae treatment
  25. --Shmulik 02.09.2014 Add Ranked values
  26. --Shmulik 08.07.2015 fixed bug (missing sector) in line 449
  27. --Moran 28.10.2015: filter out non-users where RedeemersOnly=1 (instead of in MailingChannels)
  28. --Moran 29.10.2015: filter out accounts with only 1 coupon (in "RankedXs" calculation)
  29. --Ella 18.04.2016: Replace Delete by truncate from Tnam_Functions for performance
  30. --Moran 19.07.2017: valid deals completion in research mode also; insert dealcode=-1 to  Tp_Update_Padj_Factor_History table when there are no valid deals
  31.  
  32. BEGIN TRY
  33. BEGIN TRAN
  34.  
  35. --ALTER  procedure [dbo].[Pp_CollectedDataByP] (
  36. declare
  37. @DealCode INT = 45,
  38.                                             @RnDFunctionCode INT = 1005,
  39.                                             @Ptype INT = 0,
  40.                                             @Pparams INT = 1
  41. --) AS
  42.  
  43. select count(*) as A
  44. from FLConnect_Imp.dbo.zzzTnam_Functions_Backup
  45. where FunctionCode=1005
  46.     and CouponCode=2019021270 and Ptype=-1 and DealCode=45
  47.  
  48. SET NOCOUNT ON
  49.  
  50. --DECLARE @DealCode INT=209,@RnDFunctionCode INT = NULL,@Ptype INT = 0,@Pparams INT = NULL
  51. exec Pcont_WriteToLog @DealCode,'Start P ','Pp_CollectedDataByP'
  52. declare @Padj_FactorDealsBack INT,@Padj_FactorFromDeal int
  53. declare @CouponFilterCode int
  54. declare @ChannelScopeFilter INT
  55. DECLARE @PrintDayPurchase bit
  56. declare @FunctionCode SMALLINT
  57. DECLARE @BCusage bit
  58.  
  59. declare @I smallint,  @S smallint ,@SearchDealCode int, @P smallint,@IsFinal smallint,@Sql varchar(500)
  60.    
  61. --Gather coupon Information
  62. create table #Coupons(
  63. CouponCode int,
  64. Description varchar(250),
  65. OfferType SMALLINT)
  66.  
  67. insert #Coupons
  68. Select CouponCode,Description,OfferType
  69. from Tcpn_MainHistory
  70. where DealCode=@DealCode
  71.  
  72. --insert 2 rows for summary entries
  73. Insert  #Coupons(CouponCode,Description) select 0,'All Coupons'
  74. Insert  #Coupons(CouponCode,Description) select -1,'All Coupons-Weighted Avg'
  75.  
  76. ------------------------Filtering-------------------------
  77. create table #TempFilteredCoupons
  78. (DealCode int,CouponCode int)
  79.  
  80. create table #FilteredCoupons
  81. (FunctionCode SMALLINT ,CouponCode int)
  82.  
  83. declare @StrFilterCode varchar(200)
  84. declare @TempFilterCode smallint
  85. declare @CharPosition smallint
  86. declare @FilterSQL varchar(1000)
  87. DECLARE @SQL VARCHAR(1000)
  88.  
  89. SELECT @FunctionCode=1
  90. WHILE @FunctionCode<=4
  91. BEGIN
  92.     SELECT @CouponFilterCode=CouponFilterCode,@ChannelScopeFilter=ChannelScopeFilter
  93.     FROM dbo.Tp_researchParamsByP WHERE FunctionCode=@FunctionCode
  94.  
  95.     INSERT INTO #TempFilteredCoupons (DealCode,CouponCode)
  96.     EXEC Pnam_GetFilteredCoupons @DealCode,@CouponFilterCode,@ChannelScopeFilter
  97.  
  98.     INSERT INTO #FilteredCoupons   ( FunctionCode, CouponCode )
  99.     SELECT @FunctionCode,CouponCode
  100.     FROM #TempFilteredCoupons
  101.  
  102.     --TRUNCATE TABLE #TempFilteredCoupons
  103.     delete from #TempFilteredCoupons
  104.  
  105. SELECT @FunctionCode=@FunctionCode+1
  106. END -- end  While @FunctionCode<=4
  107.  
  108.  
  109. --Create cross functionXSector information
  110. -- this table will inclurd 4 (functions)X2 (NotFinal/Final)X6(Sectors 0-5)
  111. create table #FunctionSectorCross(
  112. FunctionCode smallint,
  113. Ptype bit,
  114. Sector smallint)
  115. Select @I=1
  116. while @I<=8
  117. begin
  118.         select @IsFinal= (@I+1) % 2,
  119.         @S =0
  120.         while @S<=5
  121.             begin
  122.                     insert  #FunctionSectorCross Select CEILING(@I*1.0/2),@IsFinal,@S
  123.            
  124.                     select @S=@S+1
  125.             end
  126.     Select @I=@I+1
  127. end
  128.  
  129.  
  130.  
  131. --find last @Padj_FactorDealsBack VALID deals
  132. -- Search for deals that are representative and valid
  133. Create table #ValidDeals
  134. (FunctionCode SMALLINT,DealCode INT,OriginalFunc INT)
  135.  
  136.  
  137. SELECT @FunctionCode=1
  138. WHILE @FunctionCode<=4
  139. Begin
  140.  
  141.         select @I=0,@SearchDealCode=@DealCode
  142.         SELECT @Padj_FactorDealsBack=PAdjFactor_DealsBack,@Padj_FactorFromDeal=PAdjFactor_fromDeal FROM dbo.Tp_researchParamsByP WHERE FunctionCode=@FunctionCode
  143.  
  144.         While @I<@Padj_FactorDealsBack and @SearchDealCode>=@Padj_FactorFromDeal
  145.         begin  
  146.                 if (Select COUNT(*) from Tmng_DebriefParams inner join Tdeal_HeaderPPB on
  147.                                 Tmng_DebriefParams.DealCode=Tdeal_HeaderPPB.DealCode
  148.                                     Where IsRepresentative=1 and ISNULL(DataStatus,0) IN(0,1,6) and Tmng_DebriefParams.DealCode=@SearchDealCode)>0
  149.                                         begin
  150.                                         insert #ValidDeals (FunctionCode,DealCode,OriginalFunc) select @FunctionCode, @SearchDealCode,@FunctionCode
  151.                                         select @I=@I+1
  152.                                         end
  153.             Select @SearchDealCode=@SearchDealCode-1
  154.         END
  155.  
  156.  
  157.         SELECT @FunctionCode=@FunctionCode+1
  158. END         --WHILE @FunctionCode<=4
  159. --merge valid deals and current deals into required deals
  160.  
  161.  
  162.  
  163.  
  164. Create table #RequiredDeals
  165. (DealCode int)
  166.  
  167. insert #RequiredDeals
  168. select DISTINCT DealCode from #ValidDeals
  169.  
  170. if (Select COUNT(*) from #RequiredDeals where DealCode=@DealCode )=0
  171. begin
  172. insert #RequiredDeals
  173. Select @DealCode
  174. end
  175.  
  176. CREATE TABLE #Tp_researchParamsByP(
  177.     FunctionCode smallint,
  178.     CouponFilterCode int,
  179.     PadjFactor_ChannelScope smallint,
  180.     PAdjFactor_DealsBack smallint,
  181.     PAdjFactor_fromDeal int ,
  182.     PAdj_ResearchFunc INT,
  183.     PAdj_ResearchFuncFromDeal INT,
  184.     Padj_RobustP float ,
  185.     Padj_Max float ,
  186.     Padj_MinIssue int ,
  187.     Padj_MinRed int ,
  188.     ChannelScopeFilter smallint ,
  189.     ApplyWarnings bit,
  190.     UpdatePAdjFactor BIT,
  191.     PrintDayPurchase BIT,
  192.     RedeemersOnly bit )
  193.  
  194.  
  195. ---------------------------PRODUCTION MODE-------------------------------
  196. IF @RnDFunctionCode IS NULL AND @Ptype=0 AND @Pparams IS NULL
  197. BEGIN  
  198.  
  199.  
  200. --get all relevalnt data from dbrf table
  201.  
  202. Select a.DealCode,AccountCode,CouponCode,a.ChannelCode,Sector,P1,FinalP1,P2,FinalP2,P3,FinalP3,P4,FinalP4,
  203.                 CASE WHEN CouponUsedTimes>0 then 1 ELSE 0 END CouponUsed,
  204.                 CASE WHEN CBarCodeUsed>0 then 1 ELSE 0 END BCUsed,
  205. TotalPrintDay$Bruto
  206. into #DbrfData
  207. from Tdbrf_PersonalCoupons_Ver2 a inner join #RequiredDeals b
  208. ON a.DealCode=b.DealCode
  209. where  TotalValidPeriod$Bruto>0 and IsTargeted=1 and IsGiven=1
  210.  
  211. select count(*) as B
  212. from FLConnect_Imp.dbo.zzzTnam_Functions_Backup
  213. where FunctionCode=1005
  214.     and CouponCode=2019021270 and Ptype=-1 and DealCode=45
  215.  
  216. exec Pcont_WriteToLog @DealCode,'Delete from Tnam_Functions ','Pp_CollectedDataByP'
  217.  
  218. UPDATE STATISTICS Tnam_Functions
  219.  
  220.  
  221.  
  222. select *
  223. into #UserDifinedFunctions
  224. from FLConnect_Imp.dbo.zzzTnam_Functions_Backup_2
  225. where FunctionCode>4
  226.  
  227. --truncate table Tnam_Functions
  228. delete from FLConnect_Imp.dbo.zzzTnam_Functions_Backup_2
  229.  
  230. insert into FLConnect_Imp.dbo.zzzTnam_Functions_Backup_2
  231. select * from #UserDifinedFunctions
  232.  
  233. --UPDATE STATISTICS Tnam_Functions
  234. --Delete FROM Tnam_Functions WHERE FunctionCode BETWEEN 1 AND 4
  235.  
  236. exec Pcont_WriteToLog @DealCode,'End Delete from Tnam_Functions ','Pp_CollectedDataByP'
  237.  
  238. select count(*) as C
  239. from FLConnect_Imp.dbo.zzzTnam_Functions_Backup
  240. where FunctionCode=1005
  241.     and CouponCode=2019021270 and Ptype=-1 and DealCode=45
  242.  
  243. Select @I=1,@IsFinal=0
  244.  
  245. While @I<=8
  246. begin
  247. Select @P=CEILING(@I*1.0/2)
  248. select @IsFinal= (@I+1) % 2
  249. SELECT @ChannelScopeFilter=ChannelScopeFilter,@PrintDayPurchase=PrintDayPurchase,@BCusage=BCusage
  250. FROM dbo.Tp_researchParamsByP WHERE FunctionCode=@P
  251.  
  252.  
  253. select @Sql='Insert FLConnect_Imp.dbo.zzzTnam_Functions_Backup_2 (DealCode,AccountCode,CouponCode,FunctionCode,Ptype,P,ChannelCode,Sector,CouponUsed)
  254.              Select DealCode,AccountCode,CouponCode,' + CONVERT(varchar,@P)+
  255.                                                     ',' +CONVERT(varchar,@IsFinal) +
  256.                                                         case when @IsFinal=1 then ',FinalP' else ',P' end+CONVERT(varchar,@P)+
  257.                                                         ',ChannelCode,Sector,' + CASE WHEN @BCusage=1 THEN 'BCUsed' ELSE 'CouponUsed' END +' from #DbrfData where '
  258.                                                         + case when @IsFinal=1 then 'FinalP' else 'P' end+CONVERT(varchar,@P)+ ' is not null'
  259.                     IF @ChannelScopeFilter<>0
  260.                     BEGIN                                                                                  
  261.                     SELECT @Sql=@Sql +' AND ( ChannelCode & ' + CONVERT(VARCHAR,@ChannelScopeFilter) + '>0)'
  262.                     END
  263.                    
  264.                     IF @PrintDayPurchase=1
  265.                     BEGIN
  266.                     SELECT @Sql=@Sql + ' AND TotalPrintDay$Bruto>0'
  267.                     END
  268.                    
  269.  
  270. EXEC (@Sql)                                        
  271.  
  272. select @I=@I+1
  273. END
  274.  
  275. select count(*) as D
  276. from FLConnect_Imp.dbo.zzzTnam_Functions_Backup
  277. where FunctionCode=1005
  278.     and CouponCode=2019021270 and Ptype=-1 and DealCode=45
  279.  
  280. INSERT INTO #Tp_researchParamsByP
  281. SELECT  FunctionCode, CouponFilterCode, PadjFactor_ChannelScope, PAdjFactor_DealsBack,
  282. PAdjFactor_fromDeal, PAdj_ResearchFunc, PAdj_ResearchFuncFromDeal, Padj_RobustP, Padj_Max, Padj_MinIssue, Padj_MinRed,
  283. ChannelScopeFilter, ApplyWarnings, UpdatePAdjFactor, PrintDayPurchase, RedeemersOnly
  284. FROM dbo.Tp_researchParamsByP
  285.  
  286. END ELSE -----------------------------RND MODE-------------------------------
  287. BEGIN
  288.     IF NOT EXISTS(SELECT * FROM FLConnect_Imp.dbo.zzzTnam_Functions_Backup_2
  289.         WHERE DealCode=@DealCode AND Ptype=@Ptype AND FunctionCode=@RnDFunctionCode)
  290.         BEGIN
  291.             RAISERROR ('No data for RnD mode in Tnam_Functions', 16,1)
  292.             goto Finish
  293.         END
  294.  
  295.     INSERT INTO #Tp_researchParamsByP
  296.     SELECT  FunctionCode, CouponFilterCode, PadjFactor_ChannelScope, PAdjFactor_DealsBack,
  297. PAdjFactor_fromDeal, PAdj_ResearchFunc, PAdj_ResearchFuncFromDeal, Padj_RobustP, Padj_Max, Padj_MinIssue, Padj_MinRed,
  298. ChannelScopeFilter, ApplyWarnings, UpdatePAdjFactor, PrintDayPurchase, RedeemersOnly
  299.  FROM Tp_researchParamsByP WHERE FunctionCode=@Pparams
  300.    
  301.     UPDATE #Tp_researchParamsByP
  302.     SET FunctionCode=@RnDFunctionCode
  303.    
  304.     IF NOT EXISTS(SELECT * FROM #Tp_researchParamsByP)
  305.     BEGIN
  306.             RAISERROR ('No data for Pparams in Tp_researchParamsByP', 16,1)
  307.             goto Finish
  308.     END
  309.    
  310.    
  311.     Update #FilteredCoupons
  312.     set FunctionCode=@RnDFunctionCode where FunctionCode=@Pparams
  313.    
  314.     update #FunctionSectorCross
  315.     set FunctionCode=@RnDFunctionCode where FunctionCode=@Pparams
  316.    
  317.     Update #ValidDeals
  318.     set FunctionCode=@RnDFunctionCode,OriginalFunc=@RnDFunctionCode where FunctionCode=@Pparams
  319.    
  320.     DELETE FROM #ValidDeals WHERE FunctionCode<>@RnDFunctionCode
  321. END
  322.  
  323.  
  324.  
  325. --Filter out non-users where RedeemersOnly=1
  326.  
  327. CREATE TABLE #NonUsers
  328. (DealCode INT,AccountCode NUMERIC(18,0),ChannelCode smallint)
  329.  
  330. insert into #NonUsers
  331. select a.DealCode,AccountCode,a.ChannelCode
  332. from FLConnect_Imp.dbo.zzzTnam_Functions_Backup_2 a inner join  #Tp_researchParamsByP b
  333. on a.FunctionCode=b.FunctionCode
  334. where RedeemersOnly=1
  335. GROUP BY a.DealCode,AccountCode,a.ChannelCode
  336. HAVING MAX(CouponUsed)=0
  337.  
  338. DELETE a
  339. FROM FLConnect_Imp.dbo.zzzTnam_Functions_Backup_2 a INNER JOIN #NonUsers b ON
  340. a.AccountCode = b.AccountCode AND a.ChannelCode = b.ChannelCode AND a.DealCode = b.DealCode
  341.  
  342.  
  343.  
  344. --Add ResearchFunc data In case there are less than PAdjFactor_DealsBack deals
  345. SELECT b.FunctionCode, c.DealCode,c.FunctionCode ResearchFunc
  346. INTO #AddDeals
  347. FROM #Tp_researchParamsByP b
  348. INNER JOIN (SELECT FunctionCode,DealCode FROM dbo.FLConnect_Imp.dbo.zzzTnam_Functions_Backup_2 WHERE Ptype=0 GROUP BY FunctionCode,DealCode) c
  349. ON b.PAdj_ResearchFunc=c.FunctionCode AND PAdj_ResearchFuncFromDeal<=c.DealCode
  350. inner join Tdeal_HeaderPPB d on c.DealCode=d.DealCode
  351. where IsRepresentative=1
  352. ORDER BY b.FunctionCode,c.DealCode DESC
  353.  
  354.  
  355. declare @PAdj_ResearchFuncFromDeal INT, @PAdj_ResearchFunc INT ,@FuncDeals INT
  356.  
  357. SELECT @FunctionCode=1
  358. WHILE @FunctionCode<=4
  359. Begin
  360.         SELECT @Padj_FactorDealsBack=PAdjFactor_DealsBack,@PAdj_ResearchFuncFromDeal=PAdj_ResearchFuncFromDeal,@PAdj_ResearchFunc=PAdj_ResearchFunc
  361.         FROM #Tp_researchParamsByP WHERE FunctionCode=isnull(@RnDFunctionCode, @FunctionCode)
  362.         SELECT @FuncDeals=COUNT(*) from #ValidDeals WHERE OriginalFunc=isnull(@RnDFunctionCode, @FunctionCode)
  363.        
  364.         IF @FuncDeals<@Padj_FactorDealsBack
  365.         BEGIN
  366.             INSERT #ValidDeals
  367.             SELECT TOP (@Padj_FactorDealsBack-@FuncDeals) ResearchFunc,DealCode,FunctionCode
  368.             FROM #AddDeals WHERE FunctionCode=isnull(@RnDFunctionCode, @FunctionCode)
  369.         END
  370.  
  371.         SELECT @FunctionCode=@FunctionCode+1
  372. END         --WHILE @FunctionCode<=4
  373.  
  374.  
  375.  
  376. --Padj_Factor data : relevant only at coupon level
  377. Create table #PAdj_CouponSector(
  378. CouponCode int,
  379. FunctionCode smallint,
  380. Ptype SMALLINT,
  381. Sector smallint,
  382. CouponIssued int,
  383. CouponRed int,
  384. PEstRed float)
  385.  
  386. --create couponXsector1-5 Padj_Factor data
  387. -- on mailing channels filter out non users
  388. insert #PAdj_CouponSector
  389. select
  390. CouponCode,
  391. OriginalFunc FunctionCode,
  392. Ptype,
  393. Sector,
  394. COUNT(*) CouponIssued,
  395. SUM(CouponUsed) CouponRed,
  396. SUM(P) PEstRed
  397. from FLConnect_Imp.dbo.zzzTnam_Functions_Backup_2 a inner join #ValidDeals b ON
  398. a.DealCode=b.DealCode AND a.FunctionCode=b.FunctionCode
  399. WHERE Ptype IN (@Ptype,1)
  400. group by CouponCode,OriginalFunc,Ptype,Sector
  401.  
  402. --create couponXsector0 Padj_Factor data
  403. insert #PAdj_CouponSector
  404. select
  405. CouponCode,
  406. FunctionCode,
  407. Ptype,
  408. 0,
  409. sum(CouponIssued) CouponIssued,
  410. SUM(CouponRed) CouponRed,
  411. SUM(PEstRed) PEstRed
  412. from #PAdj_CouponSector
  413. where Sector<>5
  414. group by CouponCode,FunctionCode,Ptype
  415.  
  416.  
  417.  
  418.  
  419. --Calculate decile
  420. Create table #CouponSectorDecile
  421. (DealCode int,
  422. AccountCode numeric(18,0),
  423. CouponCode int,
  424. FunctionCode smallint,
  425. Ptype SMALLINT,
  426. ChannelCode smallint,
  427. Sector smallint,
  428. P float,
  429. Decile smallint,
  430. CouponUsed SMALLINT,
  431. Ranking smallint)
  432.  
  433. --create couponXsextor1-5 decile
  434. insert #CouponSectorDecile
  435. Select
  436. a.DealCode,
  437. AccountCode,
  438. CouponCode,
  439. a.FunctionCode,
  440. Ptype,
  441. ChannelCode,
  442. Sector,
  443. P,
  444. ntile(10) over (Partition by CouponCode,a.FunctionCode,Ptype,Sector order by P desc,convert(bigint,AccountCode) % 999 ,AccountCode) Decile,
  445. CouponUsed,
  446. ROW_NUMBER() OVER (Partition by a.FunctionCode,AccountCode,Ptype,ChannelCode order by P DESC,CouponCode) Ranking
  447. from FLConnect_Imp.dbo.zzzTnam_Functions_Backup_2 a
  448. where a.DealCode=@DealCode AND Ptype IN (@Ptype,1)
  449.  
  450. --create couponXsextor0 decile
  451. insert #CouponSectorDecile
  452. Select
  453. a.DealCode,
  454. AccountCode,
  455. CouponCode,
  456. a.FunctionCode,
  457. Ptype,
  458. ChannelCode,
  459. 0,
  460. P,
  461. ntile(10) over (Partition by CouponCode,a.FunctionCode,Ptype order by P desc,convert(bigint,AccountCode) % 999,AccountCode) Decile,
  462. CouponUsed,
  463. ROW_NUMBER() OVER (Partition by a.FunctionCode,AccountCode,Ptype,ChannelCode order by P DESC,CouponCode) Ranking
  464. from FLConnect_Imp.dbo.zzzTnam_Functions_Backup_2 a
  465. where a.DealCode=@DealCode and Sector<>5 AND Ptype IN (@Ptype,1)
  466.  
  467.  
  468. --create sextor1-4 decile
  469. insert #CouponSectorDecile
  470. Select
  471. DealCode,
  472. AccountCode,
  473. 0,
  474. FLConnect_Imp.dbo.zzzTnam_Functions_Backup_2.FunctionCode,
  475. Ptype,
  476. ChannelCode,
  477. Sector,
  478. P,
  479. ntile(10) over (Partition by FLConnect_Imp.dbo.zzzTnam_Functions_Backup_2.FunctionCode,Ptype,Sector order by P desc,convert(bigint,AccountCode) % 999,AccountCode) Decile,
  480. CouponUsed,
  481. ROW_NUMBER() OVER (Partition by FLConnect_Imp.dbo.zzzTnam_Functions_Backup_2.FunctionCode,AccountCode,Ptype,ChannelCode,Sector order by P DESC,FLConnect_Imp.dbo.zzzTnam_Functions_Backup_2.CouponCode) Ranking
  482. from FLConnect_Imp.dbo.zzzTnam_Functions_Backup_2 INNER JOIN  #FilteredCoupons ON
  483. FLConnect_Imp.dbo.zzzTnam_Functions_Backup_2.FunctionCode=#FilteredCoupons.FunctionCode AND
  484. FLConnect_Imp.dbo.zzzTnam_Functions_Backup_2.CouponCode=#FilteredCoupons.CouponCode
  485.  where DealCode=@DealCode  AND Ptype IN (@Ptype,1)
  486.  
  487.  
  488.  
  489. --create sextor0 decile
  490. insert #CouponSectorDecile
  491. Select
  492. DealCode,
  493. AccountCode,
  494. 0,
  495. FLConnect_Imp.dbo.zzzTnam_Functions_Backup_2.FunctionCode,
  496. Ptype,
  497. ChannelCode,
  498. 0,
  499. P,
  500. ntile(10) over (Partition by FLConnect_Imp.dbo.zzzTnam_Functions_Backup_2.FunctionCode,Ptype order by P desc,convert(bigint,AccountCode) % 999,AccountCode) Decile,
  501. CouponUsed,
  502. ROW_NUMBER() OVER (Partition by FLConnect_Imp.dbo.zzzTnam_Functions_Backup_2.FunctionCode,AccountCode,Ptype,ChannelCode order by P DESC,FLConnect_Imp.dbo.zzzTnam_Functions_Backup_2.CouponCode) Ranking
  503. from FLConnect_Imp.dbo.zzzTnam_Functions_Backup_2 INNER JOIN  #FilteredCoupons ON
  504. FLConnect_Imp.dbo.zzzTnam_Functions_Backup_2.FunctionCode=#FilteredCoupons.FunctionCode AND
  505. FLConnect_Imp.dbo.zzzTnam_Functions_Backup_2.CouponCode=#FilteredCoupons.CouponCode
  506. where DealCode=@DealCode and Sector<>5 AND Ptype IN (@Ptype,1)
  507.  
  508.  
  509. --summarize data
  510.  
  511. --calc num coupons per account in order to filter out accounts with only 1 coupon
  512. select AccountCode, COUNT(Distinct CouponCode) NumCoupons
  513. into #NumCoupons
  514. from #CouponSectorDecile
  515. group by AccountCode
  516.  
  517. Select
  518. CouponCode,
  519. FunctionCode,
  520. Ptype,
  521. Sector,
  522. COUNT(*)  CouponIssued,
  523. SUM(CouponUsed) CouponRed,
  524. SUM(P) PEstRed,
  525. SUM(case when Decile<=1 then 1 else 0 end) CouponIssuedDR10,
  526. SUM(case when Decile<=1 then CouponUsed else 0 end) CouponRedDR10,
  527. SUM(case when Decile<=2 then 1 else 0 end) CouponIssuedDR20,
  528. SUM(case when Decile<=2 then CouponUsed else 0 end) CouponRedDR20,
  529. SUM(case when Decile<=4 then 1 else 0 end) CouponIssuedDR40,
  530. SUM(case when Decile<=4 then CouponUsed else 0 end) CouponRedDR40,
  531. SUM(case when Decile<=8 then 1 else 0 end) CouponIssuedDR80,
  532. SUM(case when Decile<=8 then CouponUsed else 0 end) CouponRedDR80,
  533.  
  534. SUM(CASE WHEN Ranking=1 and b.NumCoupons>1 THEN CouponUsed ELSE 0 END) CouponRedRanked1,
  535. SUM(CASE WHEN Ranking=1 and b.NumCoupons>1 THEN 1 ELSE 0 END) CouponIssuedRanked1,
  536. SUM(CASE WHEN Ranking<=2 and b.NumCoupons>1 THEN CouponUsed ELSE 0 END) CouponRedRanked2,
  537. SUM(CASE WHEN Ranking<=2 and b.NumCoupons>1 THEN 1 ELSE 0 END) CouponIssuedRanked2,
  538. SUM(CASE WHEN Ranking<=3 and b.NumCoupons>1 THEN CouponUsed ELSE 0 END) CouponRedRanked3,
  539. SUM(CASE WHEN Ranking<=3 and b.NumCoupons>1 THEN 1 ELSE 0 END) CouponIssuedRanked3
  540. into #CouponSectorLevel
  541. from #CouponSectorDecile a inner join #NumCoupons b
  542. on a.AccountCode=b.AccountCode
  543. group by CouponCode,FunctionCode,Ptype,Sector
  544.  
  545.  
  546.  
  547.  
  548. --calculate DR,Rank
  549. Select
  550. CouponCode,
  551. FunctionCode,
  552. Ptype,
  553. Sector,
  554. CouponIssued,
  555. CouponRed,
  556. PEstRed,
  557. Case when CouponRed>0 and PEstRed>0 then
  558.                     CouponRed/PEstRed else null end AccuracyRatio,
  559. case    when CouponRed>0 and        CouponIssuedDR10>0  and CouponIssued>0 then        
  560. 100*(1.0*CouponRedDR10/CouponIssuedDR10)/(1.0*CouponRed/CouponIssued) else null end DR10,
  561. case    when CouponRed>0 and        CouponIssuedDR20>0  and CouponIssued>0 then        
  562. 100*(1.0*CouponRedDR20/CouponIssuedDR20)/(1.0*CouponRed/CouponIssued) else null end DR20,
  563. case    when CouponRed>0 and        CouponIssuedDR40>0  and CouponIssued>0 then        
  564. 100*(1.0*CouponRedDR40/CouponIssuedDR40)/(1.0*CouponRed/CouponIssued) else null end DR40,
  565. case    when CouponRed>0 and        CouponIssuedDR80>0 and CouponIssued>0 then     
  566. 100*(1.0*CouponRedDR80/CouponIssuedDR80)/(1.0*CouponRed/CouponIssued) else null end DR80
  567.                     into #DRAccuracy
  568.  from #CouponSectorLevel
  569.  
  570.  
  571. -- create weigthed avarage information
  572. select
  573. -1 CouponCode,
  574. #DRAccuracy.FunctionCode,
  575. Ptype,
  576. Sector,
  577. sum(DR10*CouponRed)/SUM(CouponRed) DR10,
  578. sum(DR20*CouponRed)/SUM(CouponRed) DR20,
  579. sum(DR40*CouponRed)/SUM(CouponRed) DR40,
  580. sum(DR80*CouponRed)/SUM(CouponRed) DR80
  581. into #TempWeightedAverage
  582. from #DRAccuracy  INNER JOIN  #FilteredCoupons ON
  583. #DRAccuracy.FunctionCode=#FilteredCoupons.FunctionCode AND
  584. #DRAccuracy.CouponCode=#FilteredCoupons.CouponCode
  585. where #DRAccuracy.CouponCode>0
  586. group by #DRAccuracy.FunctionCode,
  587. Ptype,
  588. Sector
  589.  
  590.  
  591. insert #DRAccuracy
  592. Select b.CouponCode,
  593. b.FunctionCode,
  594. b.Ptype,
  595. b.Sector,
  596. a.CouponIssued,
  597. a.CouponRed,
  598. a.PEstRed,
  599. a.AccuracyRatio,
  600. b.DR10,
  601. b.DR20,
  602. b.DR40,
  603. b.DR80
  604. from #DRAccuracy a inner join #TempWeightedAverage b on
  605. a.FunctionCode=b.FunctionCode and
  606. a.Ptype=b.Ptype and
  607. a.Sector=b.Sector
  608. where a.CouponCode=0
  609.  
  610.  
  611. --calculate DR,Rank  and accuracy data
  612. Select
  613. CouponCode,
  614. FunctionCode,
  615. Ptype,
  616. Sector,
  617. CouponIssuedRanked1,
  618. CouponRedRanked1,
  619. CouponIssuedRanked2,
  620. CouponRedRanked2,
  621. CouponIssuedRanked3,
  622. CouponRedRanked3,
  623. CASE WHEN CouponIssuedRanked1>0 THEN
  624. 100*(1.0*CouponRedRanked1/CouponIssuedRanked1) ELSE 0 END CouponRanked1,
  625. CASE WHEN CouponIssuedRanked2>0 THEN
  626. 100*(1.0*CouponRedRanked2/CouponIssuedRanked2) ELSE 0 END CouponRanked2,
  627. CASE WHEN CouponIssuedRanked3>0 THEN
  628. 100*(1.0*CouponRedRanked3/CouponIssuedRanked3) ELSE 0 END CouponRanked3
  629.                     into #Ranking
  630.  from #CouponSectorLevel
  631.  
  632.  
  633.  
  634.  
  635. delete Tp_resultsSummary
  636. FROM dbo.Tp_resultsSummary INNER JOIN dbo.#Tp_researchParamsByP ON
  637. dbo.Tp_resultsSummary.FunctionCode = dbo.#Tp_researchParamsByP.FunctionCode AND
  638. dbo.Tp_resultsSummary.CouponFilterCode = dbo.#Tp_researchParamsByP.CouponFilterCode AND
  639. dbo.Tp_resultsSummary.PadjFactor_DealsBack = dbo.#Tp_researchParamsByP.PAdjFactor_DealsBack
  640. where DealCode=@DealCode
  641.  
  642.  
  643. insert Tp_resultsSummary
  644. (DealCode, FunctionCode, IsFinal, CouponCode, CouponDescription,
  645. OfferType, CouponFilterCode, PadjFactor_DealsBack, Sector,
  646. CouponIssued, CouponRed, CouponIssuedForPadjFactor,CouponRedForPadjFactor,PEstRed, PEstRedForPadjFactor, AccuracyRatio,
  647. PTempAdjFactor, DR10, DR20, DR40, DR80,Ranked1,Ranked2,Ranked3)
  648. Select
  649. @DealCode,
  650. #Tp_researchParamsByP.FunctionCode,
  651. #FunctionSectorCross.Ptype,
  652. #Coupons.CouponCode,
  653. #Coupons.Description,
  654. #Coupons.OfferType,
  655. CouponFilterCode,
  656. PAdjFactor_DealsBack,
  657. #FunctionSectorCross.Sector,
  658. #DRAccuracy.CouponIssued,
  659. #DRAccuracy.CouponRed,
  660. #PAdj_CouponSector.CouponIssued,
  661. #PAdj_CouponSector.CouponRed,
  662. ROUND(#DRAccuracy.PEstRed,2),
  663. ROUND(#PAdj_CouponSector.PEstRed,2),
  664. ROUND(#DRAccuracy.AccuracyRatio,2),
  665. ROUND(Case when  isnull(#PAdj_CouponSector.PEstRed,0)>0 then    
  666.                     1.0*(#PAdj_CouponSector.CouponRed+Padj_RobustP)/(   #PAdj_CouponSector.PEstRed+Padj_RobustP) else null END,2) PtempAdjFactor,
  667. ROUND(#DRAccuracy.DR10,0),
  668. ROUND(#DRAccuracy.DR20,0),
  669. ROUND(#DRAccuracy.DR40,0),
  670. ROUND(#DRAccuracy.DR80,0),
  671. ROUND(#Ranking.CouponRanked1,2),
  672. ROUND(#Ranking.CouponRanked2,2),
  673. ROUND(#Ranking.CouponRanked3,2)
  674. from (#Coupons cross join #FunctionSectorCross)
  675. INNER JOIN #Tp_researchParamsByP ON
  676. #FunctionSectorCross.FunctionCode=#Tp_researchParamsByP.FunctionCode
  677. left join #PAdj_CouponSector on
  678. #Coupons.CouponCode=#PAdj_CouponSector.CouponCode and
  679. #FunctionSectorCross.FunctionCode =#PAdj_CouponSector.FunctionCode and
  680. #FunctionSectorCross.Ptype =(CASE WHEN #PAdj_CouponSector.Ptype IN (-1,0) THEN 0 ELSE 1 END) and
  681. #FunctionSectorCross.Sector=#PAdj_CouponSector.Sector
  682. left join #DRAccuracy on
  683. #Coupons.CouponCode=#DRAccuracy.CouponCode and
  684. #FunctionSectorCross.FunctionCode =#DRAccuracy.FunctionCode and
  685. #FunctionSectorCross.Ptype =(CASE WHEN #DRAccuracy.Ptype IN (-1,0) THEN 0 ELSE 1 END) and
  686. #FunctionSectorCross.Sector=#DRAccuracy.Sector
  687. left join #Ranking on
  688. #Coupons.CouponCode=#Ranking.CouponCode and
  689. #FunctionSectorCross.FunctionCode =#Ranking.FunctionCode and
  690. #FunctionSectorCross.Ptype =(CASE WHEN #Ranking.Ptype IN (-1,0) THEN 0 ELSE 1 END) and
  691. #FunctionSectorCross.Sector=#Ranking.Sector
  692.  
  693.  
  694. --calculate PadjFactor
  695. Update Tp_resultsSummary
  696. set PadjFactor= ROUND(case when PTempAdjFactor>Padj_Max then Padj_Max
  697.                                             when PTempAdjFactor<1/Padj_Max then 1/Padj_Max
  698.                                             else PTempAdjFactor END,2)
  699. from    Tp_resultsSummary  INNER JOIN dbo.#Tp_researchParamsByP ON
  700. dbo.Tp_resultsSummary.FunctionCode = dbo.#Tp_researchParamsByP.FunctionCode AND
  701. dbo.Tp_resultsSummary.CouponFilterCode = dbo.#Tp_researchParamsByP.CouponFilterCode AND
  702. dbo.Tp_resultsSummary.PadjFactor_DealsBack = dbo.#Tp_researchParamsByP.PAdjFactor_DealsBack
  703. where DealCode=@DealCode
  704.                            
  705.  
  706.  
  707. --calculate ABSErrorAvg
  708. select
  709. #DRAccuracy.FunctionCode,
  710. Ptype,
  711. Sector,
  712. sum((Case when CouponRed>0 and PEstRed>0 then
  713.         case when CouponRed>PEstRed then
  714.                     1.0*CouponRed/PEstRed -1
  715.         else    1.0*PEstRed/CouponRed -1    end
  716. else null end)  *CouponRed)/SUM(CouponRed)  ABSErrorAvg    
  717. into #ABSError
  718. from #DRAccuracy  INNER JOIN  #FilteredCoupons ON
  719. #DRAccuracy.FunctionCode=#FilteredCoupons.FunctionCode AND
  720. #DRAccuracy.CouponCode=#FilteredCoupons.CouponCode
  721. where #DRAccuracy.CouponCode>0
  722. group by #DRAccuracy.FunctionCode,
  723. Ptype,
  724. Sector
  725.  
  726.  
  727. Update Tp_resultsSummary
  728. set ABSErrorAvg=ROUND(b.ABSErrorAvg,2)
  729. from Tp_resultsSummary INNER JOIN dbo.#Tp_researchParamsByP ON
  730. dbo.Tp_resultsSummary.FunctionCode = dbo.#Tp_researchParamsByP.FunctionCode AND
  731. dbo.Tp_resultsSummary.CouponFilterCode = dbo.#Tp_researchParamsByP.CouponFilterCode AND
  732. dbo.Tp_resultsSummary.PadjFactor_DealsBack = dbo.#Tp_researchParamsByP.PAdjFactor_DealsBack
  733.  inner join #ABSError b on
  734. Tp_resultsSummary.FunctionCode=b.FunctionCode and
  735. Tp_resultsSummary.IsFinal=(CASE WHEN b.Ptype IN (-1,0) THEN 0 ELSE 1 END) and
  736. Tp_resultsSummary.Sector=b.Sector
  737. where DealCode=@DealCode
  738. and Tp_resultsSummary.CouponCode in (-1,0)
  739.  
  740. --Insert to Tp_UpdatePadjFactorHistory
  741. DECLARE @LastDeal INT,@FromDeal INT,@NumDeals INT
  742. SELECT @LastDeal=MAX(DealCode),@FromDeal=MIN(DealCode),@NumDeals=COUNT(DISTINCT DealCode)
  743. FROM #ValidDeals
  744.  
  745. DELETE FROM Tp_UpdatePadjFactorHistory WHERE UpdateTime IS NULL
  746.  
  747. INSERT INTO dbo.Tp_UpdatePadjFactorHistory
  748.         ( FunctionCode ,DealCode ,FromDeal ,DealsNum,
  749.           ResearchFunc ,ResearchFuncFromDeal ,CouponFilterCode ,ChannelScope )
  750. SELECT FunctionCode,isnull(@LastDeal,-1),isnull(@FromDeal,-1),@NumDeals,PAdj_ResearchFunc,PAdj_ResearchFuncFromDeal,
  751. CouponFilterCode,ChannelScopeFilter
  752. FROM #Tp_researchParamsByP WHERE UpdatePAdjFactor=1
  753.  
  754. DROP TABLE #Coupons,#TempFilteredCoupons,#FilteredCoupons,#FunctionSectorCross,#ValidDeals,
  755. #RequiredDeals,#Tp_researchParamsByP,#NonUsers,#AddDeals,#NumCoupons
  756.  
  757. select count(*) as E
  758. from FLConnect_Imp.dbo.zzzTnam_Functions_Backup
  759. where FunctionCode=1005
  760.     and CouponCode=2019021270 and Ptype=-1 and DealCode=45
  761.  
  762. --EXEC Pp_ValidationByP @DealCode, @RnDFunctionCode,@Pparams
  763.  
  764. exec Pcont_WriteToLog @DealCode,'End P ','Pp_CollectedDataByP'
  765.  
  766.  
  767. ROLLBACK TRAN
  768. END TRY
  769. BEGIN CATCH
  770.     declare @sMsg_ varchar(255) = error_message()
  771.     raiserror (@sMsg_, 16, 1);
  772.    
  773.     if @@TRANCOUNT > 0 ROLLBACK TRAN
  774. END CATCH
  775.  
  776. Finish:
  777.     if @@TRANCOUNT > 0 ROLLBACK TRAN
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement