Guest User

Untitled

a guest
Jul 18th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. USE MSIEnterprise
  2.  
  3. DECLARE @AvgProfitMargin DECIMAL(5,2)
  4. SET @AvgProfitMargin = (SELECT CAST((SUM(ClientTotal) - SUM(FieldRepTotal))/ SUM(ClientTotal) AS DECIMAL(5,2))
  5. FROM WorkOrder)
  6.  
  7. SELECT f.RepCode AS 'Rep Code'
  8.  
  9. --Number of orders a rep has worked since Infinity Rollout
  10. ,COUNT(*) AS 'Orders'
  11.  
  12. --Avg number of orders a rep works per day since hire date or Infinity Rollout
  13. ,CONVERT(DECIMAL(7,2),COUNT(*)) /
  14. CONVERT(DECIMAL(7,2),
  15. (GETDATE() - (CASE
  16. WHEN f.ActiveDate > '04/01/2010 00:00:000'
  17. THEN f.ActiveDate
  18. ELSE '04/01/2010 00:00:000'
  19. END))) AS 'Orders Per Day'
  20.  
  21. /*Calculates a Rep's initial 'open order limit' by taking their existing
  22. Orders per day and multiplying by 7 days, the idea in the future is to have this
  23. auto-adjusted based on their performance or 'Rating' */
  24. ,CEILING((CONVERT(DECIMAL(7,2),COUNT(*)) /
  25. CONVERT(DECIMAL(7,2),
  26. (GETDATE() - (CASE
  27. WHEN f.ActiveDate > '04/01/2010 00:00:000'
  28. THEN f.ActiveDate
  29. ELSE '04/01/2010 00:00:000'
  30. END))) * 7)) AS 'Order Limit'
  31.  
  32.  
  33.  
  34. --Avg amount of time it takes a Rep to return work
  35. ,AVG(CONVERT(DECIMAL(7,2),(w.RepTurnTime))) AS 'Avg Turn Time'
  36.  
  37. --Number of orders a Rep submits which, at any point in the lifecycle, are Rejected
  38. ,SUM(CASE
  39. WHEN wc.Comment LIKE '%HAS BEEN REJECTED%'
  40. THEN 1
  41. ELSE 0
  42. END) AS '# Rejected'
  43.  
  44. --Percent of a Rep's work that gets Rejected at any point in the lifecycle
  45. ,SUM(CASE
  46. WHEN wc.Comment LIKE '%HAS BEEN REJECTED.%'
  47. THEN 1
  48. ELSE 0
  49. --have to convert COUNT(*) to floating point variable or function will return ZERO
  50. END) / CONVERT(DECIMAL(7,2),COUNT(*)) * 100 AS '% Rejected'
  51.  
  52. --MSI profitability on a Rep's body of work (ClientTotal - RepTotal)/(ClientTotal)
  53. ,CAST((SUM(w.ClientTotal) - SUM(w.FieldRepTotal))/
  54. SUM(w.ClientTotal) AS DECIMAL(5,2)) AS 'Profit Margin'
  55.  
  56. --Percent above the company-wide average profit margin
  57. ,CAST((SUM(w.ClientTotal) - SUM(w.FieldRepTotal))/
  58. SUM(w.ClientTotal) AS DECIMAL(5,2)) - @AvgProfitMargin AS '% Above Avg Profit Margin'
  59.  
  60. --Rep's earned turn time points toward Total Rating
  61. ,(15 - (AVG(CONVERT(DECIMAL(7,2),(w.RepTurnTime))))) * 3.3 AS 'TurnTime Rating(of 50)'
  62.  
  63. --Rep's earned Rejected % points toward Total Rating
  64. ,((10-(SUM(CASE
  65. WHEN wc.Comment LIKE '%HAS BEEN REJECTED.%'
  66. THEN 1
  67. ELSE 0
  68. END) / CONVERT(DECIMAL(7,2),COUNT(*))* 100)) * 2.5) AS 'Rejected Rating(of 25)'
  69.  
  70. --Bonus/Penalty based on Rep's profit margin
  71. ,((CAST((SUM(w.ClientTotal) - SUM(w.FieldRepTotal)) / --NOTE: this weight chosen because a rep will rarely have a rating above 25 (translates to 63%+ profit margin)
  72. SUM(w.ClientTotal) AS DECIMAL(5,2))) * 100 * .4) AS 'Profit Margin Rating (of 40)'
  73.  
  74. /*Algorithm with weighted values to calculate a 'Rating'
  75. SIMPLE ((15 - AVGTURNTIME)*5) + ((10 - (%REJECTED * 100)*2.5)
  76. WITH PROFIT MARGIN ((15 - AVGTURNTIME)*3.3) + ((10 - (%REJECTED * 100)*2.5) + ((PROFITMARGIN) * 100 * .4) */
  77. ,(15 - (AVG(CONVERT(DECIMAL(7,2),(w.RepTurnTime))))) * 3.3 +
  78. ((10-(SUM(CASE
  79. WHEN wc.Comment LIKE '%HAS BEEN REJECTED.%'
  80. THEN 1
  81. ELSE 0
  82. END) / CONVERT(DECIMAL(7,2),COUNT(*))* 100)) * 2.5) +
  83. ((CAST((SUM(w.ClientTotal) - SUM(w.FieldRepTotal)) /
  84. SUM(w.ClientTotal) AS DECIMAL(7,2)) ) * 100 * .4) AS 'Total Rating'
  85.  
  86.  
  87. FROM WorkOrder w WITH (NOLOCK) INNER JOIN FieldRep f WITH (NOLOCK)
  88. ON w.idFieldRep = f.idFieldRep
  89. INNER JOIN WorkOrderComment wc WITH (NOLOCK)
  90. ON wc.idWorkOrder = w.idWorkOrder
  91.  
  92. WHERE f.RepCode != 'TX5536'
  93. --and w.LastActiveDate > MONTH(GETDATE()-2)
  94. GROUP BY f.RepCode, f.ActiveDate
  95. HAVING COUNT(*) > 100
  96. ORDER BY 'Total Rating' Desc
Add Comment
Please, Sign In to add comment