Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. WITH CTE_Revenue AS(
  2. SELECT c.Clinic,
  3. c.ClinicID,
  4. SUM(td.Amount)*-1 AS Revenue,
  5. p.PeriodID,
  6. p.FiscalYear
  7. FROM Trans.TransactionHeader th
  8. JOIN Clinic.[Master] c
  9. ON (th.ClinicID = c.ClinicID)
  10. JOIN Trans.TransactionDetail td
  11. ON (th.ClinicID = td.ClinicID AND th.TranNum = td.TranNum)
  12. JOIN Clinic.EOD e
  13. ON (th.ClinicID = e.ClinicID AND th.TranNum BETWEEN e.StartTran AND e.EndTran)
  14. JOIN Clinic.Period p
  15. ON (CAST(e.TimeRan AS date) BETWEEN p.PeriodStart AND p.PeriodEnd)
  16. AND th.Impacts='C'
  17. GROUP BY c.Clinic, c.ClinicID, p.PeriodID, p.FiscalYear)
  18.  
  19. SELECT w.Clinic,
  20. w.Revenue,
  21. (w.Revenue / days.CurrentDays) AS DailyAverage,
  22. (w.Revenue / days.CurrentDays)*d.PeriodDays AS ProjectedRevenue
  23. FROM CTE_Revenue w
  24. JOIN Clinic.Dates d
  25. ON (w.ClinicID = d.ClinicID AND w.PeriodID = d.PeriodID AND w.FiscalYear = d.FiscalYear)
  26. JOIN ( SELECT DISTINCT
  27. td.ClinicID,
  28. COUNT(DISTINCT td.DateEntered) AS CurrentDays,
  29. p.PeriodID,
  30. p.FiscalYear
  31. FROM Trans.TransactionDetail td
  32. JOIN Clinic.Period p
  33. ON td.DateEntered BETWEEN p.PeriodStart AND p.PeriodEnd
  34. GROUP BY td.ClinicID, p.PeriodID, p.FiscalYear) AS days
  35. ON (w.ClinicID = days.ClinicID AND w.PeriodID=days.PeriodID AND w.FiscalYear = days.FiscalYear)
  36. WHERE w.FiscalYear = 2016
  37. AND w.PeriodID = 9
  38.  
  39. SELECT w.Clinic,
  40. w.Revenue,
  41. (w.Revenue / days.CurrentDays) AS DailyAverage,
  42. (w.Revenue / days.CurrentDays)*d.PeriodDays AS ProjectedRevenue,
  43. PrevYear.Revenue,
  44. (PrevYear.Revenue / PrevYear.CurrentDays) AS PYDailyAverage,
  45. (PrevYear.Revenue / PrevYear.CurrentDays)*d.PeriodDays AS PYCalculated
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement