Advertisement
DataCCIW

Attendance Type Settings Overview

Sep 4th, 2020 (edited)
2,072
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 4.85 KB | None | 0 0
  1. USE [ArenaDB]
  2. GO
  3.  
  4. /****** Object:  View [dbo].[cust_CCIW_v_occurence_type]    Script Date: 9/22/2020 3:00:50 PM ******/
  5. SET ANSI_NULLS ON
  6. GO
  7.  
  8. SET QUOTED_IDENTIFIER ON
  9. GO
  10.  
  11.  
  12. -- Meaning of core_occurrence_type_template.occurrence_freq_type and freq_qualifier
  13. ------------------------------------------------------------------------------------
  14. -- Undefined = -1
  15. -- Daily = 0        freq_qualifier: ""
  16. -- Weekly = 1;      freq_qualifier: 0=Sun, 1=M, 2=Tu, 3=Wed, ... , 6=Sat (string)
  17. -- Monthly = 2      freq_qualifier: 1=first day of month; 31=last day of month (string)
  18. -- OneTime = 3      freq_qualifier: date (string)
  19.  
  20. CREATE VIEW [dbo].[cust_CCIW_v_occurence_type]
  21. AS
  22. WITH occurence_frequencies
  23. AS (
  24.     SELECT T1.occurrence_type_id
  25.         ,STUFF((
  26.                 SELECT ', ' + T2.schedule_name + ' (' +
  27.                     (CASE occurrence_freq_type
  28.                         WHEN  0 THEN 'Daily'
  29.                         WHEN  1 THEN 'Weekly'
  30.                         WHEN  2 THEN 'Monthly'
  31.                         WHEN  3 THEN 'OneTime' END)
  32.                     + ')'
  33.                 FROM core_occurrence_type_template T2
  34.                 WHERE T2.occurrence_type_id = T1.occurrence_type_id
  35.                 FOR XML PATH('')
  36.                 ), 1, 1, '') AS Freqs
  37.     FROM core_occurrence_type_template T1
  38.     GROUP BY T1.occurrence_type_id
  39.     HAVING occurrence_type_id IS NOT NULL
  40.     ),
  41.  
  42. occurrence_labels
  43. AS (
  44.     SELECT T1.occurrence_type_id
  45.         ,STUFF((
  46.                 SELECT ', ' + T2.report_name
  47.                 FROM cust_CCIW_v_occurrence_labels T2
  48.                 WHERE T2.occurrence_type_id = T1.occurrence_type_id
  49.                 FOR XML PATH('')
  50.                 ), 1, 1, '') AS labels
  51.     FROM cust_CCIW_v_occurrence_labels T1
  52.     GROUP BY T1.occurrence_type_id
  53.     HAVING occurrence_type_id IS NOT NULL
  54.     ),
  55. available_kiosks
  56. AS (
  57. SELECT lt.occurrence_type_id
  58.         ,STUFF((
  59.                 SELECT ', ' + s.notes
  60.                 FROM [orgn_location_occurrence_type] lt2
  61.                 JOIN comp_system_location sl ON lt2.location_id = sl.location_id
  62.                 JOIN comp_system s ON s.system_id = sl.system_id
  63.                 WHERE lt2.occurrence_type_id = lt.occurrence_type_id
  64.                 FOR XML PATH('')
  65.                 ), 1, 1, '') AS available_kiosks
  66.     FROM [orgn_location_occurrence_type] lt
  67.     JOIN comp_system_location sl ON lt.location_id = sl.location_id
  68.     GROUP BY lt.occurrence_type_id
  69. ),
  70.  
  71. available_locations
  72. AS (
  73.     SELECT lt.occurrence_type_id
  74.         ,STUFF((
  75.                 SELECT ', ' + l.location_name
  76.                 FROM orgn_location_occurrence_type lt2
  77.                 JOIN orgn_location l ON lt2.location_id = l.location_id
  78.                 WHERE lt.occurrence_type_id = lt2.occurrence_type_id
  79.                 FOR XML PATH('')
  80.                 ), 1, 1, '') AS available_locations
  81.     FROM [orgn_location_occurrence_type] lt
  82.     GROUP BY lt.occurrence_type_id
  83. )    
  84.  
  85. SELECT TOP 10000 t.occurrence_type_id -- used top here so that order by could be used
  86.     ,t.type_name
  87.     ,t.group_id
  88.     ,g.group_name
  89.     ,Active = t.active
  90.     ,[Settings Review] = CASE
  91.         WHEN membership_required = 1
  92.             AND COALESCE(sync_with_profile, sync_with_cluster, sync_with_group, NULL) IS NULL
  93.             THEN 'Membership_required but no syncing object specified. '
  94.         ELSE ''
  95.         END
  96.     ,count_as_weekend_attendance = t.is_service
  97.     ,count_as_volunteer_attendance = CASE -- This custom to the way CCIW indicates volunteer attendance
  98.         WHEN profile_source_luid = 11592
  99.             THEN 1
  100.         ELSE 0
  101.         END
  102.     ,t.membership_required
  103.     ,membership_required_in = p.profile_name
  104.     ,v.profile_name AS leader_tag
  105.     ,v.profile_id AS leader_tag_id
  106.     ,profile_id = t.sync_with_profile
  107.     ,location_specific_occurrences_enabled = t.location_specific_occurrences
  108.     ,OL.labels
  109.     ,[Uses_Override_Label] = CASE WHEN S.occurrence_type_id is not null  THEN 'Yes' ELSE 'No' END
  110.     ,OCF.Freqs
  111.      ,AL.available_locations
  112.     ,AK.available_kiosks
  113.     ,CASE
  114.         WHEN a.t IS NULL
  115.             THEN 0
  116.         ELSE a.t
  117.         END AS total_attendance
  118.  
  119. FROM core_occurrence_type t
  120. JOIN core_occurrence_type_group g ON g.group_id = t.group_id
  121. LEFT JOIN core_profile p ON p.profile_id = t.sync_with_profile
  122. LEFT JOIN core_occurrence_type_leader l ON l.occurrence_type_id = t.occurrence_type_id
  123. LEFT JOIN core_profile v ON v.profile_id = l.profile_id
  124. LEFT JOIN occurrence_labels OL ON OL.occurrence_type_id = t.occurrence_type_id
  125. LEFT JOIN occurence_frequencies OCF on OCF.occurrence_type_id = t.occurrence_type_id
  126. LEFT JOIN (
  127.     SELECT DISTINCT OL2.occurrence_type_id
  128.     FROM cust_CCIW_v_occurrence_labels OL2
  129.     WHERE OL2.shared_filter = 'overridden'
  130.     ) S ON S.occurrence_type_id = t.occurrence_type_id
  131. LEFT JOIN (
  132.     SELECT occurrence_type
  133.         ,SUM(attendance) AS t
  134.     FROM core_v_occurrence
  135.     GROUP BY occurrence_type
  136.     ) AS a ON a.occurrence_type = t.occurrence_type_id
  137. LEFT JOIN available_locations AL ON AL.occurrence_type_id = T.occurrence_type_id
  138. LEFT JOIN available_kiosks AK ON AK.occurrence_type_id = T.occurrence_type_id
  139. ORDER BY group_name, type_name
  140. GO
  141.  
  142.  
  143.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement