Guest User

Untitled

a guest
May 23rd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.47 KB | None | 0 0
  1. CREATE PROCEDURE [dbo].[csp_Build_PivotTableCache]
  2. AS
  3.  
  4. BEGIN TRANSACTION
  5. SELECT TOP 0 * FROM tbPivotTableUpdates_Cache WITH ( TABLOCKX )
  6.  
  7. TRUNCATE TABLE tbPivotTableUpdates_Cache
  8.  
  9. INSERT INTO tbPivotTableUpdates_Cache
  10. SELECT
  11. FP.FiscalQuarter,
  12. SH.Svp,
  13. SH.SvpName,
  14. SH.Vp,
  15. SH.VpName,
  16. SH.SrDirector,
  17. SH.SrDirectorName,
  18. SH.Director,
  19. SH.DirectorName,
  20. Team.TeamNumber,
  21. Team.TeamDescription,
  22. Customer.CustomerNumber,
  23. Customer.CustomerDescription,
  24. ISNULL(Product.BrandDescription, 'NA') AS BrandDescription,
  25. AllProducts.ProductNumber,
  26. ISNULL(Product.ProductDescription, 'NA') AS ProductDescription,
  27. ISNULL(Reference.Volume, 0) AS PriorYearVolume,
  28. CONVERT(Decimal(30, 8), CASE
  29. WHEN ISNULL(Allocation.Volume, 0) = 0 THEN 0.0001
  30. ELSE ISNULL(Allocation.Volume, 0)
  31. END) AS AllocationVolume,
  32. ISNULL(SalesPlan.Volume, 0) AS PlanVolume,
  33. ISNULL(Reference.WorkingTrade, 0) AS PriorYearWorkingTradeRate,
  34. ISNULL(Allocation.WorkingTrade, 0) AS AllocationWorkingTradeRate,
  35. ISNULL(SalesPlan.WorkingTrade, 0) AS PlanWorkingTradeRate,
  36. ISNULL(SalesPlan.Volume, 0) * ISNULL(Allocation.Rate, 0) AS ProjectedFunding,
  37. ISNULL(Reference.GrossSales, 0) AS PriorYearGrossSales,
  38. ISNULL(SalesPlan.GrossSales, 0) AS PlanGrossSales,
  39. ISNULL(Reference.GrossSales - Reference.WorkingTrade, 0) AS PriorYearNetSales,
  40. ISNULL(SalesPlan.GrossSales - SalesPlan.WorkingTrade, 0) AS PlanNetSales
  41. FROM
  42. (
  43. SELECT DISTINCT
  44. CustomerNumber
  45. FROM st_RateAllocation_FY09H2.dbo.tbCustomers
  46. ) UserCustomers
  47. CROSS JOIN
  48. (
  49. SELECT LEFT(EPD.PGNo, 7) AS ProductNumber
  50. FROM tbEventPlan_Detail_Backup EPD
  51. LEFT JOIN tbBrand_Exclude_ForReport BE ON LEFT(EPD.PGNO,2) = BE.BrandId
  52. WHERE BE.BrandId IS NULL
  53.  
  54. UNION
  55.  
  56. SELECT LEFT(EPD.PGNo, 7) AS ProductNumber
  57. FROM vwEventPlan_Reference EPD
  58. LEFT JOIN tbBrand_Exclude_ForReport BE ON LEFT(EPD.PGNO,2) = BE.BrandId
  59. WHERE BE.BrandId IS NULL
  60.  
  61. UNION
  62.  
  63. SELECT FullProductId AS ProductNumber
  64. FROM st_RateAllocation_FY09H2.dbo.vwSmoothingExtractByPa
  65. LEFT JOIN tbBrand_Exclude_ForReport BE ON LEFT(FullProductId,2) = BE.BrandId
  66. WHERE BE.BrandId IS NULL
  67. ) AllProducts
  68. CROSS JOIN
  69. (
  70. SELECT
  71. [Quarter] AS FiscalQuarter,
  72. MIN(StartDate) AS StartDate,
  73. MAX(EndDate) AS EndDate
  74. FROM tbFiscalPeriod FP
  75. WHERE [Year] = '2009' AND [Quarter] IN ('3', '4')
  76. GROUP BY [Quarter]
  77. ) FP
  78. LEFT JOIN
  79. (
  80. SELECT
  81. FiscalQuarter,
  82. '00' + EP.PANo AS CustomerNumber,
  83. LEFT(EP.PGNo, 7) AS ProductNumber,
  84. SUM(EP.TotalVolumeCSU) AS Volume,
  85. SUM(EP.CAGGrossSalesDollars) AS GrossSales,
  86. SUM(EP.WorkingTrade) AS WorkingTrade
  87. FROM vwEventPlan EP
  88. GROUP BY
  89. FiscalQuarter,
  90. '00' + EP.PANo,
  91. LEFT(EP.PGNo, 7)
  92. ) SalesPlan
  93. ON FP.FiscalQuarter = SalesPlan.FiscalQuarter
  94. AND UserCustomers.CustomerNumber = SalesPlan.CustomerNumber
  95. AND AllProducts.ProductNumber = SalesPlan.ProductNumber
  96. LEFT JOIN
  97. (
  98. SELECT
  99. 3 AS FiscalQuarter,
  100. R.CustomerNumber,
  101. R.ProductNumber,
  102. FLOOR(R.ProfitLossVolume / 2) AS Volume,
  103. CONVERT(Decimal(30, 8), CASE
  104. WHEN R.ProfitLossVolume = 0 THEN CONVERT(Decimal(30, 8), R.TotalRate) * CONVERT(Decimal(30, 8), 0.0001)
  105. ELSE R.TotalRate * R.ProfitLossVolume / 2
  106. END) AS WorkingTrade,
  107. R.TotalRate AS Rate
  108. FROM st_RateAllocation_FY09H2.dbo.tbRates R
  109. UNION ALL
  110. SELECT
  111. 4 AS FiscalQuarter,
  112. R.CustomerNumber,
  113. R.ProductNumber,
  114. FLOOR(R.ProfitLossVolume / 2)
  115. + CASE
  116. WHEN R.ProfitLossVolume % 2 = 1 THEN 1
  117. ELSE 0
  118. END AS Volume,
  119. CONVERT(Decimal(30, 8), CASE
  120. WHEN R.ProfitLossVolume = 0 THEN CONVERT(Decimal(30, 8), R.TotalRate) * CONVERT(Decimal(30, 8), 0.0001)
  121. ELSE R.TotalRate * R.ProfitLossVolume / 2
  122. END) AS WorkingTrade,
  123. R.TotalRate AS Rate
  124. FROM st_RateAllocation_FY09H2.dbo.tbRates R
  125. ) Allocation
  126. ON FP.FiscalQuarter = Allocation.FiscalQuarter
  127. AND UserCustomers.CustomerNumber = Allocation.CustomerNumber
  128. AND AllProducts.ProductNumber = Allocation.ProductNumber
  129. LEFT JOIN
  130. (
  131. SELECT
  132. FiscalQuarter,
  133. '00' + Ref.PANo AS CustomerNumber,
  134. LEFT(Ref.PGNo, 7) AS ProductNumber,
  135. SUM(Ref.TotalVolume_CSU) AS Volume,
  136. SUM(Ref.CAGGrossSalesDollars) AS GrossSales,
  137. SUM(Ref.WorkingTrade) AS WorkingTrade
  138. FROM vwEventPlan_Reference Ref
  139. GROUP BY
  140. FiscalQuarter,
  141. '00' + Ref.PANo,
  142. LEFT(Ref.PGNo, 7)
  143. ) Reference
  144. ON FP.FiscalQuarter = Reference.FiscalQuarter
  145. AND UserCustomers.CustomerNumber = Reference.CustomerNumber
  146. AND AllProducts.ProductNumber = Reference.ProductNumber
  147. LEFT JOIN
  148. (
  149. SELECT
  150. MAX(LVL1_ID) AS BrandID,
  151. MAX(RTRIM(LVL1_DESC)) AS BrandDescription,
  152. LVL3_ID AS ProductNumber,
  153. MAX(RTRIM(LVL3_DESC)) AS ProductDescription
  154. FROM st_SharedMaster.dbo.tbSAP_Hierarchy_Product
  155. GROUP BY
  156. LVL3_ID
  157. ) Product
  158. ON AllProducts.ProductNumber = Product.ProductNumber
  159. JOIN st_RateAllocation_FY09H2.dbo.tbTeamsCustomers TeamsCustomers
  160. ON UserCustomers.CustomerNumber = TeamsCustomers.CustomerNumber
  161. JOIN st_RateAllocation_FY09H2.dbo.tbTeams Team
  162. ON TeamsCustomers.TeamNumber = Team.TeamNumber
  163. JOIN st_RateAllocation_FY09H2.dbo.tbCustomers Customer
  164. ON UserCustomers.CustomerNumber = Customer.CustomerNumber
  165. JOIN
  166. (
  167. SELECT DISTINCT
  168. SH.LVL3_ID AS Svp,
  169. SH.LVL3_NAME AS SvpName,
  170. CASE
  171. WHEN TC.TeamNumber IN(SH.LVL3_ID) THEN '-'
  172. ELSE SH.LVL4_ID
  173. END AS Vp,
  174. CASE
  175. WHEN TC.TeamNumber IN(SH.LVL3_ID) THEN '-'
  176. ELSE SH.LVL4_NAME
  177. END AS VpName,
  178. CASE
  179. WHEN TC.TeamNumber IN(SH.LVL3_ID, SH.LVL4_ID) THEN '-'
  180. ELSE SH.LVL5_ID
  181. END AS SrDirector,
  182. CASE
  183. WHEN TC.TeamNumber IN(SH.LVL3_ID, SH.LVL4_ID) THEN '-'
  184. ELSE SH.LVL5_NAME
  185. END AS SrDirectorName,
  186. CASE
  187. WHEN TC.TeamNumber IN(SH.LVL3_ID, SH.LVL4_ID, SH.LVL5_ID) THEN '-'
  188. ELSE SH.LVL6_ID
  189. END AS Director,
  190. CASE
  191. WHEN TC.TeamNumber IN(SH.LVL3_ID, SH.LVL4_ID, SH.LVL5_ID) THEN '-'
  192. ELSE SH.LVL6_NAME
  193. END AS DirectorName,
  194. TC.CustomerNumber
  195. FROM st_SharedMaster.dbo.tbSAP_Hierarchy_Sales SH
  196. JOIN st_RateAllocation_FY09H2.dbo.tbTeamsCustomers TC
  197. ON TC.TeamNumber IN (SH.LVL3_ID, SH.LVL4_ID, SH.LVL5_ID, SH.LVL6_ID)
  198. ) SH
  199. ON UserCustomers.CustomerNumber = SH.CustomerNumber
  200. WHERE ISNULL(SalesPlan.Volume, 0) > 0
  201. OR ISNULL(SalesPlan.WorkingTrade, 0) > 0
  202. OR ISNULL(Allocation.Volume, 0) > 0
  203. OR ISNULL(Reference.Volume, 0) > 0
  204. OR ISNULL(Reference.WorkingTrade, 0) > 0
  205.  
  206.  
  207. UPDATE tbPivotTableUpdates_PANo SET CachedDate = GETDATE()
  208. COMMIT
  209. GO
Add Comment
Please, Sign In to add comment