Advertisement
DataCCIW

cust_CCIW_v_involvment

Jun 26th, 2020
2,282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 7.30 KB | None | 0 0
  1. CREATE VIEW [dbo].[cust_CCIW_v_involvment]
  2. AS
  3.  
  4. WITH LastAttendance AS
  5. (
  6.     SELECT  person_id, MAX(datetime_value) AS last_attendance
  7.     FROM        dbo.cust_CCIW_v_attendance
  8.     GROUP BY person_id
  9. ),
  10.  
  11. LastWebActivity AS
  12. (
  13.     SELECT person_id, last_web_activity = MAX(H.date_created)
  14.     FROM core_person_history H
  15.     GROUP BY person_id,history_type_luid
  16.     HAVING  H.history_type_luid = 108 --108 = Web Activity
  17. ),
  18.  
  19. LastNote AS
  20. (
  21.     SELECT person_id, last_note = MAX(H.date_created)
  22.     FROM core_person_history H
  23.     GROUP BY person_id,history_type_luid
  24.     HAVING  H.history_type_luid = 355 --355 = User Note
  25. ),
  26.  
  27. LastPersonUpdate AS
  28. (
  29.     SELECT person_id, last_person_update = MAX(H.date_created)
  30.     FROM core_person_history H
  31.     GROUP BY person_id,history_type_luid
  32.     HAVING  H.history_type_luid = 366 --366 = Person Update
  33. ),
  34.  
  35. LastTag AS
  36. (
  37.     SELECT person_id, last_tag_updated = MAX(PM.date_modified)
  38.     FROM core_profile_member PM
  39.     GROUP BY PM.person_id
  40. ),
  41.  
  42. LastViewed AS
  43. (
  44. SELECT [target_person_id], last_viewed = MAX([view_datetime])
  45.   FROM [core_person_viewed_by]
  46.   GROUP BY target_person_id
  47. ),
  48.  
  49. LastMemberStatusChange
  50. AS
  51. (
  52. SELECT person_id,
  53.     last_status_update = MAX(date_created)
  54. FROM core_person_history H WHERE H.history like 'Member Status changed%'
  55. GROUP BY person_id
  56. ),
  57.  
  58. DateValues AS
  59. (
  60. SELECT TodayMinus1Year = DATEADD(year, -1, GETDATE()), TodayMinus2Years = DATEADD(year, -2, GETDATE())
  61. ),
  62.  
  63. sum_giving AS
  64. (
  65. SELECT
  66.     giving_unit_id,
  67.     total_giving_last_12_months = sum(currency_amount)
  68.     -- Funds are from online giving page
  69. FROM cust_CCIW_v_ctrb_basic_filtered(Dateadd(Month, Datediff(Month, 0, DATEADD(m, -12,
  70. current_timestamp)), 0),GETDATE(),'-1','-1','-1')
  71. GROUP BY giving_unit_id
  72. ),
  73.  
  74. giving_unit_count_gifts AS
  75. (
  76. SELECT
  77.     giving_unit_id,
  78.     count_gift_last_12_months = count(contribution_id)
  79. FROM cust_CCIW_v_ctrb_basic_filtered(Dateadd(Month, Datediff(Month, 0, DATEADD(m, -12,
  80. current_timestamp)), 0),GETDATE(),'-1','-1','-1')
  81. GROUP BY giving_unit_id
  82. --Not sure why this line was added perhaps I was looking at people with more than 2 gifts at some point
  83. --HAVING count(contribution_id) >= 3
  84. ),
  85.  
  86. contribution_summary AS
  87.  
  88. (
  89. SELECT giving_unit_id,
  90. MAX(b.contribution_date) AS last_giving_unit_contribution
  91. FROM
  92. cust_CCIW_v_ctrb_basic b
  93. GROUP BY giving_unit_id
  94. ),
  95.  
  96. Involvement AS
  97. (
  98. SELECT        
  99.    P.person_id,
  100.    P.first_name,
  101.    P.last_name,
  102.    age = dbo.fn_Age(p.birth_date,GETDATE()),
  103.    --P.last_attended, --this field is not currently in use
  104.    P.date_modified,
  105.    M.family_id,
  106.    family_role = L2.lookup_value, -- Family Role
  107.    member_status_id = P.member_status,
  108.    member_status_name = L.lookup_value,
  109.    P.record_status,
  110.    record_status_text =
  111.    CASE P.record_status WHEN 0 THEN 'Active' WHEN 1 THEN 'Inactive' WHEN 2 THEN 'Pending' END,
  112.    [Meets Regular Attender Criteria] =
  113.    CASE WHEN (AC.Last_26_Weeks_Count >= 6) THEN 1 ELSE 0 END,
  114.    [Meets Occasional Attender Criteria] =
  115.    CASE WHEN ((AC.Last_52_Weeks_Count >= 1) AND (AC.Last_26_Weeks_Count < 6))  
  116.       AND (AC.Last_52_Weeks_Count >= 6) THEN 1 ELSE 0 END,
  117.    [Meets Visitor Criteria] =
  118.    CASE WHEN  AC.Last_52_Weeks_Count >= 1 AND AC.Last_52_Weeks_Count < 6 THEN 1 ELSE 0 END,
  119.    dbo.cust_CCIW_funct_is_in_SmallGroup(P.person_id, 1) AS SmallGroup,
  120.    dbo.cust_CCIW_funct_is_Serving(P.person_id, 1) AS Serving,
  121.    LA.last_attendance, -- Any attendance including non-worship service
  122.    CS.last_giving_unit_contribution,
  123.    LW.last_web_activity,
  124.    LT.last_tag_updated,
  125.    LV.last_viewed,
  126.    LN.last_note,
  127.    LPU.last_person_update,
  128.    -- last x week attendance counts only look at worship related attendance (occurence attendance, , 1st/2nd/3rd Visit Attributes, Weekend Related Giving)
  129.    SC.last_status_update,
  130.    AC.Last_52_Weeks_Count,
  131.    AC.Last_52_Weeks_Family_Count,
  132.    AC.Last_26_Weeks_Count,
  133.    AC.Last_26_Weeks_Family_Count,
  134.    AC.Last_13_Weeks_Count,
  135.    AC.Last_13_Weeks_Family_Count,
  136.    AC.Last_4_Weeks_Count,
  137.    AC.Last_4_Weeks_Family_Count,
  138.    former_status = L.lookup_qualifier4,
  139.    CG.count_gift_last_12_months,
  140.    SG.total_giving_last_12_months,      
  141.    DateValues.*        
  142. FROM
  143.    core_person AS P INNER JOIN
  144.    --cust_CCIW_v_person_basic as B on P.person_id = B.person_id INNER JOIN
  145.    core_lookup AS L ON L.lookup_id = P.member_status INNER JOIN
  146.    core_family_member M ON M.person_id = P.person_id LEFT JOIN
  147.    core_lookup L2 on L2.lookup_id = M.role_luid LEFT JOIN
  148.    LastAttendance LA on LA.person_id = P.person_id LEFT JOIN
  149.    --contribution summary groups on giving unit id vs person_id
  150.    contribution_summary AS CS ON CS.giving_unit_id = P.giving_unit_id LEFT JOIN
  151.    cust_CCIW_v_WS_attendance_counts AS AC ON AC.person_id = P.person_id LEFT JOIN
  152.    LastWebActivity LW on LW.person_id = P.person_id LEFT JOIN
  153.    LastTag LT on LT.person_id = P.person_id LEFT JOIN
  154.    LastViewed LV on LV.target_person_id = P.person_id LEFT JOIN
  155.    LastNote LN on LN.person_id = P.person_id LEFT JOIN
  156.    LastMemberStatusChange SC on SC.person_id = P.person_id LEFT JOIN
  157.    LastPersonUpdate LPU on LPU.person_id = P.person_id LEFT JOIN
  158.    sum_giving SG on SG.giving_unit_id = P.giving_unit_id LEFT JOIN
  159.    giving_unit_count_gifts CG on CG.giving_unit_id = P.giving_unit_id INNER JOIN
  160.    DateValues on 1=1
  161. ),
  162.  
  163. Analysis AS
  164. (
  165. Select                                                                         
  166.     [Involvment In Last Year] = CASE
  167.         WHEN
  168.             I.Last_52_Weeks_Family_Count >=1 OR
  169.             I.Serving = 1 OR
  170.             I.SmallGroup = 1 OR
  171.             I.count_gift_last_12_months >= 1 OR
  172.             --https://stackoverflow.com/questions/71022/sql-max-of-multiple-columns/6871572#6871572
  173.             (SELECT Max(v)
  174.                 FROM (VALUES
  175.                     (last_attendance),
  176.                     (last_giving_unit_contribution),
  177.                     (last_web_activity),
  178.                     (last_tag_updated),
  179.                     (last_note)) AS value(v)
  180.             ) >= TodayMinus1Year
  181.             THEN 1 ELSE 0 END,
  182.     [Involvment In Last 2 Years] = CASE
  183.         WHEN
  184.             I.Last_52_Weeks_Family_Count >=1 OR
  185.             I.Serving = 1 OR
  186.             I.SmallGroup = 1 OR
  187.             (SELECT Max(v)
  188.                 FROM (VALUES
  189.                     (last_attendance),
  190.                     (last_giving_unit_contribution),
  191.                     (last_web_activity),
  192.                     (last_tag_updated),
  193.                     (last_note)) AS value(v)
  194.             ) >= TodayMinus2Years
  195.             THEN 1 ELSE 0 END,
  196.     AttendanceAfterLastStatusChange = CASE WHEN I.last_attendance > I.last_status_update THEN 'Yes' ELSE 'No' END,
  197.     I.*
  198.     FROM Involvement I
  199.    
  200. )
  201.  
  202. Select
  203. [Recommended Status ID] =  
  204.         CASE
  205.             WHEN (former_status = '' OR (former_status = '1' and AttendanceAfterLastStatusChange = 'Yes'))
  206.             THEN CASE
  207.                      WHEN [Meets Regular Attender Criteria] = 1 THEN 'Regular Attender (Non-Member)'         -- 960   = Regular Attender (Non-Member)
  208.                      WHEN [Meets Occasional Attender Criteria] = 1 THEN 'Occasional Attender (Non-Member)'   -- 10415 = Occasional Attender (Non-Member)
  209.                      WHEN [Meets Visitor Criteria] = 1 THEN 'Visitor-Wheaton Campus'                         -- 961   = Visitor-Wheaton Campus
  210.                      -- 960   = Regular Attender, 10415 = Occasional Attender, 10387 = Former Attender
  211.                      WHEN member_status_id in (960,10415) AND (count_gift_last_12_months < 4 or count_gift_last_12_months is null)
  212.                            AND (total_giving_last_12_months < 500 or total_giving_last_12_months is null) Then 'Former Attender'                             
  213.                      WHEN member_status_id = 961 THEN 'Former Visitor'                                               -- 961   = Visitor-Wheaton Campus,-- 10414 = Former Visitor'
  214.                   END                                                              
  215.             END,
  216. A.* FROM Analysis A
  217.  
  218. GO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement