Guest User

Untitled

a guest
Nov 24th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.79 KB | None | 0 0
  1. USE [ABC]
  2. GO
  3.  
  4. /****** Object: View [report].[vw_MonthlyDealAllocations] Script Date: 11/24/2017 9:54:10 PM ******/
  5. --SET ANSI_NULLS ON
  6. --GO
  7.  
  8. --SET QUOTED_IDENTIFIER ON
  9. --GO
  10.  
  11. --CREATE VIEW [report].[vw_MonthlyDealAllocations] as
  12. SELECT
  13. PeriodStartDate,
  14. PeriodEndDate,
  15. D.DealID,
  16. Dl.DealName,
  17. D.StageID,
  18. CONVERT(varchar(1), D.StageID) + ' - ' + DS.Stage Stage,
  19. DP.Platform,
  20. ISNULL(D.AllocatedOfficeID, Dl.OfficeID) AS AllocatedOfficeID,
  21. ISNULL(D.AllocatedDepartmentID, Dl.DepartmentID) AS AllocatedDepartmentID,
  22. CASE
  23. WHEN ISNULL(D.AllocatedDepartmentID, Dl.DepartmentID) = 1 THEN 'Investment Advisory'
  24. WHEN ISNULL(D.AllocatedDepartmentID, Dl.DepartmentID) = 2 THEN 'Debt/EP'
  25. WHEN ISNULL(D.AllocatedDepartmentID, Dl.DepartmentID) = 6 THEN 'HFF Securities'
  26. ELSE DM.Department
  27. END AS AllocatedDepartment,
  28. AO.Name AllocatedOffice,
  29. CASE
  30. WHEN DP.DisplayWithOffice = 1 THEN AO.Name + ' ' + DP.ShortName
  31. ELSE AO.Name
  32. END AllocatedOfficeDept,
  33. Dl.DepartmentID DealDepartmentID,
  34. DX.Department DealDepartment,
  35. Dl.OfficeID DealOfficeID,
  36. DO.Name DealOffice,
  37. CASE
  38. WHEN DP.DisplayWithOffice = 1 THEN DO.Name + ' ' + DP.ShortName
  39. ELSE DO.Name
  40. END DealOfficeDept,
  41. DT.DealType,
  42. D.OfficeAllocVolumePipeline,
  43. D.OfficeAllocVolumeCompleted,
  44. D.OfficeAllocFeePipeline,
  45. D.OfficeAllocFeeCompleted,
  46. D.OfficeAllocVolumePipelineGBP,
  47. D.OfficeAllocVolumeCompletedGBP,
  48. D.OfficeAllocFeePipelineGBP,
  49. D.OfficeAllocFeeCompletedGBP,
  50. PTG.GroupName,
  51. PT.PropertyType,
  52. CASE
  53. WHEN C.CountryID <> 1 THEN 'Foreign'
  54. ELSE R.Region
  55. END AS Region,
  56. AP.Address1,
  57. C.City,
  58. S.StateLong,
  59. AP.Zip,
  60. Investor.ClientName MSA,
  61. Cl.ClientName RollupMSA,
  62. ITI.InvestorType PrimaryInvestor,
  63. ITC.InvestorType PrimaryClient,
  64. P.FirstName + ' ' + P.LastName AS PrimaryProducer,
  65. I.FirstName + ' ' + I.LastName AS IL_Producer,
  66. D.CountDealCompleted,
  67. CASE Dl.isSecuritized
  68. WHEN 0 THEN 'No'
  69. WHEN 1 THEN 'Yes'
  70. ELSE ''
  71. END AS isSecuritized,
  72. CASE Dl.isServiced
  73. WHEN 0 THEN 'No'
  74. WHEN 1 THEN 'Yes'
  75. ELSE ''
  76. END AS isServiced,
  77. --QCount.QtCt,
  78. /*IIF (CASE
  79. WHEN D.isCompleted = 1 THEN D.VolumeCompleted
  80. WHEN (D.isPipeline = 1 OR D.StageID = 0) THEN D.VolumePipeline
  81. ELSE 0
  82. END >= 50000000,1,NULL
  83. ) AS Over50M, */
  84. IIF(Dl.PortfolioID IS NOT NULL, 1, NULL) AS isPortfolio,
  85. --IIF (isMultitransactional.linktype IS NOT NULL,1,NULL) AS isMultiTransactional,
  86. DP.PlatformID,
  87. P.EmployeeID AS PrimaryProducerID,
  88. I.EmployeeID AS IL_ProducerID,
  89. --report.fnReportGetDealClients(D.DealID) Clients,
  90. --report.fnReportGetDealInvestors(D.DealID) Investors,
  91. DFM.FinancingMethod,
  92. IIF((Dl.isSecuritized = 1 AND ISNULL(ISNULL(ITID2C.InvestorType, ITI.InvestorType), 'Unknown') <> 'Agency'), 'Conduit/CMBS', ISNULL(ISNULL(ITID2C.InvestorType, ITI.InvestorType), 'Unknown')) AS InvestorType
  93.  
  94. FROM report.MonthlyDealAllocations D
  95. INNER JOIN dbo.Deals Dl
  96. ON Dl.DealID = D.DealID
  97. INNER JOIN DealTypes DT
  98. ON DT.DealTypeID = Dl.DealTypeID
  99. INNER JOIN DealPlatforms DP
  100. ON DP.PlatformID = DT.PlatformID
  101. INNER JOIN DealStages DS
  102. ON DS.StageID = D.StageID
  103. LEFT JOIN Departments DM
  104. ON DM.DepartmentID = ISNULL(D.AllocatedDepartmentID, Dl.DepartmentID)
  105. LEFT JOIN Departments DX
  106. ON DX.DepartmentID = Dl.DepartmentID
  107. LEFT JOIN Offices AO
  108. ON AO.OfficeID = ISNULL(D.AllocatedOfficeID, Dl.OfficeID)
  109. LEFT JOIN Offices DO
  110. ON DO.OfficeID = Dl.OfficeID
  111. LEFT JOIN dbo.DealProperties DPP
  112. ON D.DealID = DPP.DealID
  113. AND DPP.IsMainProperty = 1
  114. LEFT JOIN PropertyTypes PT
  115. ON PT.PropertyTypeID = DPP.PropertyTypeID
  116. LEFT JOIN PropertyTypeGroups PTG
  117. ON PTG.GroupID = PT.GroupID
  118. LEFT JOIN DealProperties AS DPZ
  119. ON D.DealID = DPZ.DealID
  120. AND DPZ.isMainProperty = 1
  121. LEFT JOIN Assets AP
  122. ON DPZ.AssetID = AP.AssetID
  123. LEFT JOIN Cities C
  124. ON C.CityID = AP.CityID
  125. LEFT JOIN States S
  126. ON S.StateID = C.StateID
  127. LEFT JOIN Regions R
  128. ON ISNULL(C.RCARegionID, ISNULL(S.RCARegionID, S.RegionID)) = R.RegionID
  129. LEFT JOIN DealsToClients DTCC
  130. ON D.DealID = DTCC.DealID
  131. AND DTCC.isPrimary = 1
  132. AND DTCC.MemberType IN (1, 3)
  133. LEFT JOIN DealsToClients DTCI
  134. ON D.DealID = DTCI.DealID
  135. AND DTCI.isPrimary = 1
  136. AND DTCI.MemberType IN (2, 4)
  137. LEFT JOIN CacheClients Cl
  138. ON DTCC.ClientID = Cl.ClientID
  139. LEFT JOIN CacheClients Investor
  140. ON DTCI.ClientID = Investor.ClientID
  141. LEFT JOIN InvestorTypes ITI
  142. ON Investor.InvestorTypeID = ITI.InvestorTypeID
  143. LEFT JOIN InvestorTypes ITC
  144. ON Cl.InvestorTypeID = ITC.InvestorTypeID
  145. --LEFT JOIN (SELECT DISTINCT D2D.DealID, D2D.LinkType FROM DealsToDeals D2D WHERE LinkType = 2) isMultitransactional ON D.DealID = isMultitransactional.DealID
  146. LEFT JOIN DealsToEmployees EP
  147. ON D.DealID = EP.DealID
  148. AND EP.MemberType = 1
  149. AND EP.isPrimary = 1
  150. LEFT JOIN DealsToEmployees EI
  151. ON D.DealID = EI.DealID
  152. AND EI.MemberType = 2
  153. AND EI.isPrimary = 1
  154. LEFT JOIN Employees P
  155. ON EP.EmployeeID = P.EmployeeID
  156. LEFT JOIN Employees I
  157. ON EI.EmployeeID = I.EmployeeID
  158. LEFT JOIN DealFinancingMethods DFM
  159. ON Dl.DealFinancingMethodID = DFM.DealFinancingMethodsID
  160. LEFT JOIN InvestorTypes AS ITID2C
  161. ON DTCI.InvestorTypeID = ITID2C.InvestorTypeID
  162. --LEFT JOIN (SELECT DealID, COUNT(QuoteID) AS QtCt FROM Quotes WHERE DateRemoved IS NULL GROUP BY DealID) QCount ON D.dealid = Qcount.DealID
  163.  
  164. USE [ABC]
  165. GO
  166.  
  167. /****** Object: Index [IX_MonthlyDealAllocations_DealID_AllocDeptID_AllocOfficeID] Script Date: 11/24/2017 11:43:00 PM ******/
  168. CREATE NONCLUSTERED INDEX [IX_MonthlyDealAllocations_DealID_AllocDeptID_AllocOfficeID] ON [report].[MonthlyDealAllocations]
  169. (
  170. [DealID] ASC,
  171. [AllocatedDepartmentID] ASC,
  172. [AllocatedOfficeID] ASC
  173. )
  174. INCLUDE ( [PeriodStartDate],
  175. [PeriodEndDate],
  176. [CountDealCompleted],
  177. [StageID]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  178. GO
Add Comment
Please, Sign In to add comment