Skip to content

Commit 71419da

Browse files
committed
Add ActiveRecordsOverTime
1 parent 6dda116 commit 71419da

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

ActiveRecordsOverTime.sql

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
-- Pckgd
2+
-- Title: Active Records Over Time
3+
-- Description: Tracks the number of active records per year based on 2025 definition of active.
4+
-- Updates from: GitHub/TenthPres/TouchPointScripts/ActiveRecordsOverTime.sql
5+
-- Author: James at Tenth
6+
7+
8+
WITH FamiliesGiving AS (
9+
SELECT YEAR(cc.ContributionDate) AS ActivityYear, p.FamilyId
10+
FROM dbo.Contribution cc WITH (NOLOCK)
11+
JOIN dbo.People p WITH (NOLOCK) ON p.PeopleId = cc.PeopleId
12+
WHERE cc.ContributionTypeId IN (1,5,8,9,10,20)
13+
AND p.IsDeceased = 0
14+
GROUP BY YEAR(cc.ContributionDate), p.FamilyId
15+
HAVING COUNT(*) >= 1
16+
),
17+
PeopleFromGiving AS (
18+
SELECT fg.ActivityYear, p.PeopleId
19+
FROM FamiliesGiving fg
20+
JOIN dbo.People p WITH (NOLOCK) ON p.FamilyId = fg.FamilyId
21+
WHERE p.IsDeceased = 0
22+
),
23+
Attenders AS (
24+
SELECT YEAR(a.MeetingDate) AS ActivityYear, a.PeopleId
25+
FROM dbo.Attend a WITH (NOLOCK)
26+
JOIN dbo.People p WITH (NOLOCK) ON p.PeopleId = a.PeopleId
27+
WHERE a.AttendanceFlag = 1
28+
AND p.IsDeceased = 0
29+
GROUP BY YEAR(a.MeetingDate), a.PeopleId
30+
HAVING COUNT(*) >= 2
31+
),
32+
LegacyRegCount AS (
33+
SELECT YEAR(rd.Stamp) AS ActivityYear, rd.UserPeopleId AS PeopleId, COUNT(*) AS rCount
34+
FROM dbo.RegistrationData rd WITH (NOLOCK)
35+
JOIN dbo.People p WITH (NOLOCK) ON rd.UserPeopleId = p.PeopleId
36+
WHERE rd.UserPeopleId IS NOT NULL AND rd.Completed = 1 AND p.IsDeceased = 0
37+
GROUP BY YEAR(rd.Stamp), rd.UserPeopleId
38+
),
39+
FormsRegCount AS (
40+
SELECT YEAR(rp.CompletedDate) AS ActivityYear, rp.PeopleId, COUNT(*) AS rCount
41+
FROM dbo.RegPeople rp WITH (NOLOCK)
42+
JOIN dbo.People p WITH (NOLOCK) ON rp.PeopleId = p.PeopleId
43+
WHERE rp.PeopleId IS NOT NULL AND rp.[Status] = 2 AND p.IsDeceased = 0
44+
GROUP BY YEAR(rp.CompletedDate), rp.PeopleId
45+
),
46+
RegistrationCount AS (
47+
SELECT ActivityYear, PeopleId, SUM(rCount) AS rCount
48+
FROM (
49+
SELECT ActivityYear, PeopleId, rCount FROM LegacyRegCount
50+
UNION ALL
51+
SELECT ActivityYear, PeopleId, rCount FROM FormsRegCount
52+
) r
53+
GROUP BY ActivityYear, PeopleId
54+
),
55+
Registrants AS (
56+
SELECT ActivityYear, PeopleId FROM RegistrationCount WHERE rCount >= 2
57+
),
58+
PPL AS (
59+
SELECT ActivityYear, PeopleId FROM PeopleFromGiving
60+
UNION ALL
61+
SELECT ActivityYear, PeopleId FROM Attenders
62+
UNION ALL
63+
SELECT ActivityYear, PeopleId FROM Registrants
64+
),
65+
DistinctPPL AS (
66+
SELECT ActivityYear, PeopleId
67+
FROM PPL
68+
GROUP BY ActivityYear, PeopleId
69+
)
70+
SELECT ActivityYear, COUNT(*) AS ActiveCount
71+
FROM DistinctPPL
72+
GROUP BY ActivityYear
73+
ORDER BY ActivityYear DESC

0 commit comments

Comments
 (0)