Guest User

Untitled

a guest
Jan 2nd, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. /* This query creates start and stop dates for delivery schedules and has all of the options in two sections,
  2. OldorCurSchID, OldOrigSchedStartDate, OldSchedEndDate or in NewScheduleID, NewSchedStartDate, NewSchedEndDate */
  3. WITH delivery AS (
  4. SELECT st.SubscriptionID,
  5. st.TranNumber,
  6. st.TranTypeID,
  7. st.ReasonCode,
  8. dsc.OldScheduleID,
  9. LAG(st.TranDate) OVER (PARTITION BY st.SubscriptionID ORDER BY st.TranDate) AS OldSchedStartDate,
  10. st.TranDate-1 AS OldSchedEndDate,
  11. dsc.NewScheduleID,
  12. st.TranDate AS NewSchedStartDate,
  13. LEAD(st.TranDate) OVER (PARTITION BY st.SubscriptionID ORDER BY st.TranDate)-1 AS NewSchedEndDate
  14. FROM SubscriptionTran AS st LEFT JOIN DeliveryScheduleChange AS dsc
  15. ON st.TranNumber = dsc.TranNumber
  16. WHERE st.TranTypeID IN ('DelivSched', 'ComboChange', 'MoveOut')
  17. AND dsc.OldScheduleID IS NOT NULL
  18. )
  19.  
  20. SELECT sub.SubscriptionID,
  21. CASE /* When there are not any delivery schedule changes, uses the current schedule from the Subscription table */
  22. WHEN delivery.OldScheduleID IS NULL THEN sub.DeliveryScheduleID
  23. ELSE delivery.OldScheduleID
  24. END AS OldorCurSchID,
  25. CASE /* When there is no start date, pulls in original start date for the customer. This doesn't mean there weren't previous schedules, just none since 1-1-09 when transactions begin*/
  26. WHEN delivery.OldSchedStartDate IS NULL THEN sub.OriginalStartDate
  27. ELSE delivery.OldSchedStartDate
  28. END AS OldOrigSchedStartDate,
  29. delivery.OldSchedEndDate,
  30. delivery.NewScheduleID,
  31. delivery.NewSchedStartDate,
  32. delivery.NewSchedEndDate,
  33. delivery.TranNumber,
  34. delivery.TranTypeID,
  35. delivery.ReasonCode,
  36. sub.Subscriber AS CurrentSubscriber,
  37. sub.DeliveryScheduleID AS CurrentDelSched,
  38. sub.OriginalStartDate
  39. FROM Subscription AS sub
  40. LEFT JOIN delivery
  41. ON sub.SubscriptionID = delivery.SubscriptionID
  42. WHERE sub.ProductID = 'PM' /* Limits to Buffalo News */
  43. ORDER BY sub.SubscriptionID
Advertisement
Add Comment
Please, Sign In to add comment