Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sudarshan-webonise/3e059f2ad70c984ce9fd9ba54f864d92 to your computer and use it in GitHub Desktop.
Save sudarshan-webonise/3e059f2ad70c984ce9fd9ba54f864d92 to your computer and use it in GitHub Desktop.
SELECT CAST(@TIME_PERIOD_START AS DATE) AS PERIOD_START,
CAST(@TIME_PERIOD_END AS DATE) AS PERIOD_END,
c.CLIENT_NAME,
p.GRP_ID,
COUNT(DISTINCT u.USR_ID) AS USER_COUNT,
COUNT(DISTINCT COALESCE(up.CHILD_ID, u.USR_ID)) AS STUDENT_COUNT
FROM CLIENTS c
INNER JOIN CLIENT_PACKAGES cp ON (c.CLIENT_ID = cp.CLIENT_ID)
INNER JOIN PACKAGES p ON (p.GRP_ID = cp.GRP_ID)
INNER JOIN GROUPS g ON (p.GRP_ID = g.GRP_ID)
INNER JOIN
(SELECT DISTINCT um.USR_ID AS USR_ID,
um.GRP_ID AS GRP_ID
FROM USER_MEMBERSHIPS um
WHERE ((um.trCTM_Archive = 0
OR um.trCTM_Archive IS NULL)
AND (EXISTS
(SELECT uma.*
FROM USER_MEMBERSHIPS_AUD uma
WHERE um.USR_ID = uma.USR_ID
AND um.GRP_ID = uma.GRP_ID
AND uma.REVTYPE = 0
AND uma.EDIT_Date < @TIME_PERIOD_END)
OR NOT EXISTS
(SELECT uma.*
FROM USER_MEMBERSHIPS_AUD uma
WHERE um.USR_ID = uma.USR_ID
AND um.GRP_ID = uma.GRP_ID
AND uma.REVTYPE = 0)))
OR (um.trCTM_Archive <> 0
AND um.trCTM_Archive IS NOT NULL
AND (um.trCTM_ArchiveDate IS NULL
OR (um.trCTM_ArchiveDate >= @TIME_PERIOD_START
AND um.trCTM_ArchiveDate < @TIME_PERIOD_END))
AND NOT EXISTS
(SELECT uma2.*
FROM USER_MEMBERSHIPS_AUD uma2
WHERE uma2.USR_ID = um.USR_ID
AND uma2.GRP_ID = um.GRP_ID
AND uma2.REVTYPE = 0
AND uma2.EDIT_DATE >= @TIME_PERIOD_START
AND uma2.EDIT_DATE < @TIME_PERIOD_END))) u ON (u.GRP_ID = g.GRP_ID)
LEFT JOIN USER_PARENT up ON (u.USR_ID = up.USR_ID)
WHERE c.CLIENT_NAME NOT LIKE 'Z -%'
AND c.CLIENT_NAME NOT LIKE 'ZZ%'
AND c.CLIENT_NAME NOT LIKE 'Z-%'
AND (g.GRP_ID = :grpid)
GROUP BY c.CLIENT_NAME,
p.GRP_ID
ORDER BY c.CLIENT_NAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment