Guest User

Untitled

a guest
Jun 22nd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1.  
  2.  
  3.  
  4. --Delete Blacklisted Allocations Table if it exists
  5. IF EXISTS (SELECT * FROM sysobjects
  6. WHERE id = OBJECT_ID(N'[dbo].[_backup_tbAllocations]')
  7. AND OBJECTPROPERTY(id, N'IsTable') = 1)
  8. DROP TABLE [dbo].[_backup_tbAllocations]
  9.  
  10. --Backup Allocations Table
  11. Select * INTO _backup_tbAllocations FROM TBALLOCATIONS
  12. -------------------------------------------------------------------
  13.  
  14.  
  15. --Delete Blacklisted Allocations Table if it exists
  16. IF EXISTS (SELECT * FROM sysobjects
  17. WHERE id = OBJECT_ID(N'[dbo].[tbAllocations_Blacklist]')
  18. AND OBJECTPROPERTY(id, N'IsTable') = 1)
  19. DROP TABLE [dbo].[tbAllocations_Blacklist]
  20. --------------------------------------------------------------------
  21.  
  22. --Create table of Blacklisted Allocations
  23. SELECT
  24. A.TimePeriodID,
  25. A.CustomerNumber,
  26. PB.ProductNumber as ProductNumber,
  27. Funding,
  28. Volume
  29. Into tbAllocations_Blacklist
  30. FROM
  31. tbAllocations A
  32. Join
  33. tbProducts_Blacklist PB
  34. on
  35. A.ProductNumber = PB.ProductNumber
  36. -------------------------------------------------------------
  37.  
  38. --Delete Blacklisted Allocations from Allocation Table
  39. DELETE
  40. A
  41. FROM
  42. tbAllocations A
  43. join
  44. tbAllocations_Blacklist AB
  45. ON
  46. AB.ProductNumber = A.ProductNumber and
  47. AB.TimePeriodID= A.TimePeriodID and
  48. AB.CustomerNumber = A.CustomerNumber
  49.  
  50. --------------------------------------------------------------
  51.  
  52.  
  53. --Delete Exception Table if exists
  54. IF EXISTS (SELECT * FROM sysobjects
  55. WHERE id = OBJECT_ID(N'[dbo].[tbVP_Blacklist_Exceptions]')
  56. AND OBJECTPROPERTY(id, N'IsTable') = 1)
  57. DROP TABLE [dbo].[tbVP_Blacklist_Exceptions]
  58. -------------------------------------------------------------------
  59.  
  60.  
  61. --Find records that will be orphaned at the BD level and insert into exception table
  62. SELECT
  63. AB.CustomerNumber,
  64. AB.ProductNumber,
  65. AB.FUNDING,
  66. AB.VOLUME
  67. INTO
  68. tbVP_Blacklist_Exceptions
  69. FROM
  70. tbAllocations_Blacklist AB
  71. Join
  72. vwCustomersHierarchy ch
  73. On
  74. ch.TimePeriodID=AB.TimePeriodID and
  75. ch.CustomerNumber=AB.CustomerNumber
  76. LEFT Join
  77. tbAllocations A
  78. ON
  79. LEFT(AB.ProductNumber,7) = LEFT(A.ProductNumber,7) and
  80. AB.TimePeriodID= A.TimePeriodID and
  81. AB.CustomerNumber = A.CustomerNumber
  82. Where
  83. CH.CustomerHierarchyLevel='2' AND
  84. A.ProductNumber is Null
  85. ORDER BY FUNDING DESC
  86.  
  87. --------------------------------------------------------------------------------------
  88.  
  89. DECLARE @DATA VARCHAR(max)
  90. DECLARE @XML XML
  91.  
  92. SET @DATA ='<Allocations>'
  93.  
  94.  
  95.  
  96.  
  97. SELECT
  98. A.TimeperiodID,
  99. A.Customernumber,
  100. A.ProductNumber,
  101. isnull(A.Volume/nullif(L3.Volume,0),0) as VolSharePerc
  102. INTO #VolShareAll
  103. From
  104. tbAllocations A
  105. JOIN
  106. (Select
  107. a.TimePeriodID,
  108. a.Customernumber,
  109. left(a.ProductNumber,7) as Level3,
  110. Sum(a.Volume) as Volume
  111. From
  112. tbAllocations A
  113. Group By
  114. a.TimePeriodID,
  115. a.CustomerNumber,
  116. Left(a.Productnumber,7)) L3
  117. ON
  118. L3.TimePeriodID=A.TimePeriodID and
  119. L3.CustomerNumber = A.CustomerNumber and
  120. L3.Level3= Left(A.productnumber,7)
  121.  
  122.  
  123. SELECT DISTINCT
  124. A.TimePeriodID,
  125. A.CustomerNumber,
  126. LEFT(A.ProductNumber, 7) AS Level3,
  127. VolShare.ProductNumber
  128. INTO #HighestVol
  129. FROM tbAllocations A
  130. CROSS APPLY
  131. (
  132. SELECT TOP 1
  133. ProductNumber
  134. FROM #VolShareAll
  135. WHERE TimePeriodID = A.TimePeriodID
  136. AND CustomerNumber = A.CustomerNumber
  137. AND LEFT(ProductNumber, 7) = LEFT(A.ProductNumber, 7)
  138. ORDER BY VolSharePerc
  139. ) VolShare
  140.  
  141.  
  142.  
  143. Select @DATA = @DATA+ '<allocation> <timePeriodId>'+CONVERT(VARCHAR(10),HV.TimePeriodID)+'</timePeriodId>
  144. <customer>'+HV.CustomerNumber+'</customer>
  145. <product>'+HV.ProductNumber+'</product>
  146. <funding>'+CONVERT(VARCHAR(MAX),HV.Funding+A.Funding)+'</funding>
  147. <volume>'+CONVERT(VARCHAR(MAX),HV.Volume + A.Volume)+'</volume>
  148. </allocation>'
  149. FROM tbAllocations A
  150. JOIN
  151. (
  152. Select DISTINCT
  153. CONVERT(VARCHAR(10),HV.CustomerNumber) AS CustomerNumber,
  154. HV.TimePeriodID,
  155. HV.ProductNumber,
  156. SUM(AB.Funding) as Funding,
  157. SUM(AB.Volume) AS Volume
  158. FROM
  159. #HighestVol HV
  160. JOIN
  161. tbAllocations_Blacklist AB
  162. ON
  163. AB.TimeperiodID=HV.TimePeriodID AND
  164. AB.CustomerNumber = CONVERT(VARCHAR(10),HV.CustomerNumber) AND
  165. LEFT(AB.ProductNumber,7)=Left(HV.ProductNumber,7)
  166. JOIN vwCustomersHierarchy CH
  167. on
  168. Ch.TimePeriodID=HV.TimePeriodID AND
  169. CH.CustomerNumber= CONVERT(VARCHAR(10),HV.CustomerNumber)
  170.  
  171. Where
  172. CH.CustomerHierarchyLevel='2' and
  173. AB.Volume>1
  174. group by
  175. HV.CustomerNumber,
  176. HV.TimePeriodID,
  177. HV.ProductNumber
  178. )HV
  179. ON
  180. A.TIMEPERIODID=HV.TimePeriodID and
  181. A.CustomerNumber=HV.CustomerNumber and
  182. A.ProductNumber = HV.ProductNumber
  183.  
  184. DROP TABLE #VolShareAll
  185. DROP TABLE #HighestVol
  186.  
  187.  
  188.  
  189. SET @DATA =@DATA +'</Allocations>'
  190. SET @XML =@DATA
  191.  
  192. EXEC dbo.csp_Allocations_Save @DATA=@XML, @USERID='DISCOPROMOGROUP'
Add Comment
Please, Sign In to add comment