Advertisement
DataCCIW

Available to Schedule SQL

May 8th, 2023 (edited)
3,115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 2.11 KB | None | 0 0
  1. --Available to Schedule
  2. IF @OccurrenceDate <> ''
  3.  
  4. BEGIN
  5.     SELECT p.Id, p.LastName + ', '+ p.NickName Name, p.Email, gr.Name AS [Group Role], rs.Value AS [Record Status], cs.Value AS [Connection Status],@OccurrenceDate as OccurrenceDate
  6.     FROM GroupMember gm
  7.     JOIN [Group] g ON gm.GroupId = g.Id
  8.     JOIN Person p ON gm.PersonId = p.Id
  9.     JOIN DefinedValue rs ON p.RecordStatusValueId = rs.Id
  10.     JOIN DefinedValue cs ON p.ConnectionStatusValueId = cs.Id
  11.     JOIN GroupTypeRole gr ON gm.GroupRoleId = gr.Id
  12.     -- Use this to exclude people based black out dates
  13.     LEFT JOIN (
  14.         SELECT pa.PersonId, excl.GroupId
  15.         FROM PersonScheduleExclusion excl
  16.         JOIN PersonAlias pa WITH(NOLOCK) ON excl.PersonAliasId = pa.Id
  17.         WHERE CONVERT(DATE, @OccurrenceDate) BETWEEN excl.StartDate AND excl.EndDate
  18.         ) AS ex ON ( ex.GroupId IS NULL OR gm.GroupId = ex.GroupId ) AND p.Id = ex.PersonId
  19.     -- Use this to exclude people who have already been scheduled/requested to serve on the date specified
  20.     LEFT JOIN (
  21.         SELECT pa.PersonId, ao.GroupId
  22.         FROM Attendance att
  23.         JOIN AttendanceOccurrence ao ON att.OccurrenceId = ao.Id
  24.         JOIN PersonAlias pa WITH(NOLOCK) ON att.PersonAliasId = pa.Id
  25.         WHERE
  26.             -- Exclude anyone who has been scheduled, declined a schedule request, or where a request is in process
  27.             -- Those who have not been scheduled have a ScheduledToAttend and RequestedToAttend Set to 0
  28.             ( att.ScheduledToAttend = 1 OR (att.ScheduledToAttend = 0 AND att.RequestedToAttend = 1) OR att.RequestedToAttend = 1 )
  29.             AND CONVERT( DATE, ao.OccurrenceDate ) = CONVERT( DATE, @OccurrenceDate )
  30.         ) a ON gm.GroupId = a.GroupId AND p.Id = a.PersonId
  31.     WHERE CONVERT( VARCHAR(100), g.Guid ) = @Group AND gm.IsArchived = 0   
  32.         AND gm.GroupMemberStatus = 1
  33.         AND ex.PersonId IS NULL
  34.         AND a.PersonId IS NULL
  35.     GROUP BY  p.Id, p.LastName, p.NickName, p.Email, gr.Name, rs.Value, cs.Value
  36.  
  37.     ORDER BY p.LastName, p.NickName
  38. END
  39. ELSE
  40. BEGIN
  41.     SELECT 'Please select an occurrence date' AS Error
  42. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement