BranchManagers.sql 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. IF OBJECT_ID ('dbo.BranchManagers','V') IS NOT NULL DROP VIEW dbo.BranchManagers
  2. GO
  3. CREATE VIEW dbo.BranchManagers
  4. AS
  5. WITH ParentChildren AS (
  6. -- Anchor member: find the initial record with the description 'Operations Branch Leaders'
  7. SELECT
  8. CompanyID, WorkGroupID, Description, ParentWGID, SortOrder, AccessRights, WaitTime,
  9. BypassEscalation, UseCalendarTime, CreatedByID, CreatedByScreenID, CreatedDateTime,
  10. LastModifiedByID, LastModifiedByScreenID, LastModifiedDateTime, 0 AS Level
  11. FROM dbo.EPCompanyTree
  12. WHERE Description = 'Operations Branch Leaders'
  13. UNION ALL
  14. -- Recursive member: find children of the current level, limit recursion to levels < 3
  15. SELECT
  16. child.CompanyID, child.WorkGroupID, child.Description, child.ParentWGID, child.SortOrder,
  17. child.AccessRights, child.WaitTime, child.BypassEscalation, child.UseCalendarTime,
  18. child.CreatedByID, child.CreatedByScreenID, child.CreatedDateTime, child.LastModifiedByID,
  19. child.LastModifiedByScreenID, child.LastModifiedDateTime, parent.Level + 1 AS Level
  20. FROM dbo.EPCompanyTree AS child
  21. INNER JOIN ParentChildren AS parent ON child.ParentWGID = parent.WorkGroupID
  22. WHERE parent.Level < 2 -- Limit recursion to levels less than 3
  23. ),
  24. children AS (
  25. SELECT *
  26. FROM ParentChildren p
  27. WHERE p.Description != 'Operations Branch Leaders'
  28. AND p.Level < 3
  29. ),
  30. -- Replace ISNUMERIC with pattern matching
  31. FolderIsManager AS (
  32. SELECT
  33. c.CompanyID, c.Level, c.Description, c.WorkGroupID, c.ParentWGID, m.ContactID
  34. FROM children c
  35. INNER JOIN dbo.EPCompanyTreeMember m ON c.CompanyID = m.CompanyID AND c.WorkGroupID = m.WorkGroupID
  36. WHERE c.Level = 1
  37. AND c.Description LIKE '%[^0-9]%' -- Description is not numeric
  38. AND m.Active = 1
  39. ),
  40. FolderManagerBranches AS (
  41. SELECT
  42. a.CompanyID,
  43. a.BAccountID AS EmployeeBAccountID,
  44. a.AcctCD AS EmployeeCD,
  45. a.AcctName AS EmployeeName,
  46. u.Username,
  47. u.PKID,
  48. b.BranchID AS BranchTableID,
  49. b.BranchCD AS BranchTableCD,
  50. br.BAccountID AS BAccountTableID,
  51. br.AcctCD AS BAccountTableCD
  52. FROM FolderIsManager m
  53. INNER JOIN dbo.Contact con ON con.CompanyID = m.CompanyID AND con.ContactID = m.ContactID
  54. INNER JOIN dbo.Users u ON u.CompanyID = con.CompanyID AND con.UserID = u.PKID
  55. INNER JOIN dbo.BAccount a ON m.CompanyID = a.CompanyID AND m.ContactID = a.DefContactID
  56. INNER JOIN children c ON c.CompanyID = m.CompanyID AND c.ParentWGID = m.WorkGroupID
  57. INNER JOIN dbo.Branch b ON b.CompanyID = c.CompanyID AND b.BranchCD = c.Description
  58. INNER JOIN dbo.BAccount br ON b.CompanyID = br.CompanyID AND b.BAccountID = br.BAccountID
  59. ),
  60. FolderIsNotManager AS (
  61. SELECT
  62. c.CompanyID, c.Level, c.Description, c.WorkGroupID, c.ParentWGID
  63. FROM children c
  64. WHERE c.Level IN (1, 2)
  65. AND c.Description NOT LIKE '%[^0-9]%' -- Description is numeric
  66. ),
  67. NotFolderManagerBranches AS (
  68. SELECT
  69. a.CompanyID,
  70. a.BAccountID AS EmployeeBAccountID,
  71. a.AcctCD AS EmployeeCD,
  72. a.AcctName AS EmployeeName,
  73. u.Username,
  74. u.PKID,
  75. b.BranchID AS BranchTableID,
  76. b.BranchCD AS BranchTableCD,
  77. br.BAccountID AS BAccountTableID,
  78. br.AcctCD AS BAccountTableCD
  79. FROM FolderIsNotManager f
  80. INNER JOIN dbo.EPCompanyTreeMember m ON f.CompanyID = m.CompanyID AND m.WorkGroupID = f.WorkGroupID
  81. INNER JOIN dbo.Contact c ON m.CompanyID = c.CompanyID AND m.ContactID = c.ContactID
  82. INNER JOIN dbo.BAccount a ON a.CompanyID = c.CompanyID AND a.DefContactID = c.ContactID
  83. INNER JOIN dbo.Users u ON u.CompanyID = c.CompanyID AND u.PKID = c.UserID
  84. INNER JOIN dbo.Branch b ON b.CompanyID = f.CompanyID AND f.Description = b.BranchCD
  85. INNER JOIN dbo.BAccount br ON b.CompanyID = br.CompanyID AND br.BAccountID = b.BAccountID
  86. )
  87. SELECT * FROM FolderManagerBranches
  88. UNION
  89. SELECT * FROM NotFolderManagerBranches