Guest User

Untitled

a guest
Aug 16th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. CREATE VIEW [dbo].[SatSurvey] AS
  2. SELECT SSV1.ID, SSV1.ClinicID, CC.ShortName, NULL as HashValue, NULL as LoadDate, CollectionDate, RecommendNPS, RecommendNPSReason, OverallExperience, Comments, NULL as StartDateTime, NULL as EndDateTime, Method,
  3. CorporateAffiliation, NULL as DoNotReport
  4. FROM SatSurveyV1 SSV1 inner join ClinicConfig CC on SSV1.ClinicID = cc.ClinicID
  5. UNION ALL
  6. SELECT SSV2.ID, SSV2.ClinicID, CC.ShortName, HashValue, LoadDate, CollectionDate, RecommendNPS, RecommendNPSReason, OverallExperience, Comments, StartDateTime, EndDateTime, Method,
  7. CorporateAffiliation, DoNotReport
  8. FROM SatSurveyV2 SSV2 inner join ClinicConfig CC on SSV2.ClinicID = cc.ClinicID
  9. UNION ALL
  10. SELECT SSV2018.ID, SSV2018.ClinicID, cc.ShortName, HashValue, LoadDate, CollectionDate, RecommendNPS, RecommendNPSReason, OverallExperience, Comments, StartDateTime, EndDateTime, Method,
  11. CorporateAffiliation, DoNotReport
  12. FROM SatSurveyV2018 SSV2018 inner join ClinicConfig CC on SSV2018.ClinicID = CC.ClinicID
  13.  
  14. CREATE PROCEDURE [dbo].[CalculateNPSOverPeriod] (
  15. @NPS decimal (5,2) output,
  16. @startDate date,
  17. @endDate date,
  18. @clinicName nvarchar(50) = NULL)
  19.  
  20. AS
  21. BEGIN
  22.  
  23. SET NOCOUNT ON;
  24.  
  25. SELECT @NPS = ((SUM(Promoters) - SUM(Detractors)) / (SUM(Responses) * 1.0) * 100.0)
  26. FROM (
  27. SELECT clinicid, YEAR(CollectionDate) Yr, MONTH(CollectionDate) Mo, COUNT(*) Promoters, 0 Neutrals, 0 Detractors, 0 Responses
  28. FROM SatSurvey
  29. WHERE CollectionDate >= @startDate
  30. AND CollectionDate < DATEADD(d, 1, @endDate)
  31. AND RecommendNPS > 8
  32. AND DoNotReport IS NULL
  33. GROUP BY ClinicID, YEAR(CollectionDate), MONTH(CollectionDate)
  34. /*
  35. UNION
  36. SELECT clinicid, YEAR(CollectionDate), MONTH(CollectionDate), 0 Promoters, COUNT(*) Neutrals, 0 Detractors, 0 Responses
  37. FROM SatSurvey
  38. WHERE CollectionDate >= @startDate
  39. AND CollectionDate < DATEADD(d, 1, @endDate)
  40. AND RecommendNPS BETWEEN 7 AND 8
  41. AND DoNotReport IS NULL
  42. GROUP BY ClinicID, YEAR(CollectionDate), MONTH(CollectionDate)
  43. */
  44. UNION
  45. SELECT clinicid, YEAR(CollectionDate), MONTH(CollectionDate), 0 Promoters, 0 Neutrals, COUNT(*) Detractors, 0 Responses
  46. FROM SatSurvey
  47. WHERE CollectionDate >= @startDate
  48. AND CollectionDate < DATEADD(d, 1, @endDate)
  49. AND RecommendNPS < 7
  50. AND DoNotReport IS NULL
  51. GROUP BY ClinicID, YEAR(CollectionDate), MONTH(CollectionDate)
  52. UNION
  53. SELECT clinicid, YEAR(CollectionDate), MONTH(CollectionDate), 0 Promoters, 0 Neutrals, 0 Detractors, COUNT(*) Responses
  54. FROM SatSurvey
  55. WHERE CollectionDate >= @startDate
  56. AND CollectionDate < DATEADD(d, 1, @endDate)
  57. AND RecommendNPS IS NOT NULL
  58. AND DoNotReport IS NULL
  59. GROUP BY ClinicID, YEAR(CollectionDate), MONTH(CollectionDate)
  60. ) p INNER JOIN ClinicConfig CC on p.ClinicID = cc.ClinicID
  61. WHERE cc.GenerateSatSurvey = 1
  62. AND (@clinicName IS NULL OR (cc.MidName LIKE @clinicName))
  63. OPTION (Recompile)
  64.  
  65. RETURN @NPS
  66.  
  67. END
Add Comment
Please, Sign In to add comment