Skip to content

Commit

Permalink
Sync: 2024-01-25 20:02:24
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelAllen committed Jan 26, 2024
1 parent b48d55b commit 3369a9a
Show file tree
Hide file tree
Showing 2 changed files with 381 additions and 19 deletions.
38 changes: 19 additions & 19 deletions Rock/Groups/latest-attendance-dates-per-group.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ tags:
- type/reporting
- topic/attendance
date created: 2020-12-04 23:24:00
date modified: 2022-01-04 10:11:16
date modified: 2024-01-24 18:49:29
---

# Latest Attendance Dates Per-group
Expand All @@ -18,36 +18,36 @@ DECLARE @GroupType int = 25; --Life Groups

WITH cte_Ranked AS (
SELECT
g.Id 'GroupId'
,ao.Id 'AttendanceOccurrenceId'
,RANK() OVER ( PARTITION BY g.Id ORDER BY ao.OccurrenceDate DESC ) 'Rank'
g.[Id] 'GroupId'
,ao.[Id] 'AttendanceOccurrenceId'
,RANK() OVER ( PARTITION BY g.[Id] ORDER BY ao.[OccurrenceDate] DESC ) 'Rank'
FROM
[Group] g
LEFT JOIN [AttendanceOccurrence] ao
ON g.Id = ao.GroupId
AND ao.DidNotOccur = 0
ON g.[Id] = ao.[GroupId]
AND ( ao.[DidNotOccur] IS NULL OR ao.[DidNotOccur] = 0 )

WHERE
g.GroupTypeId = @GroupType
AND g.IsActive = 1
AND g.IsArchived = 0
g.[GroupTypeId] = @GroupType
AND g.[IsActive] = 1
AND g.[IsArchived] = 0
)
SELECT
g.Id
,g.Name
,ao.OccurrenceDate 'Last Attendance'
g.[Id]
,g.[Name]
,ao.[OccurrenceDate] 'Last Attendance'
,NULLIF( (
SELECT COUNT( 1 )
FROM [Attendance]
WHERE
OccurrenceId = r.AttendanceOccurrenceId
AND DidAttend = 1
[OccurrenceId] = r.[AttendanceOccurrenceId]
AND [DidAttend] = 1
), 0 ) 'Attendee Count'
,ao.Notes 'Attendance Note'
,ao.[Notes] 'Attendance Note'
FROM
[cte_Ranked] r
JOIN [Group] g ON r.GroupId = g.Id
LEFT JOIN [AttendanceOccurrence] ao ON r.AttendanceOccurrenceId = ao.Id
WHERE r.Rank = 1
ORDER BY ao.OccurrenceDate DESC
JOIN [Group] g ON r.[GroupId] = g.[Id]
LEFT JOIN [AttendanceOccurrence] ao ON r.[AttendanceOccurrenceId] = ao.[Id]
WHERE r.[Rank] = 1
ORDER BY ao.[OccurrenceDate] DESC
```
Loading

0 comments on commit 3369a9a

Please sign in to comment.