-
Notifications
You must be signed in to change notification settings - Fork 5
/
Find Dormant Polaris Users.sql
66 lines (60 loc) · 2.13 KB
/
Find Dormant Polaris Users.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*Thanks to Daniel Messer
This query looks for Polaris users who haven’t logged in for a given amount of time. It helps you find users who no longer work for the library, but their account still exists in Polaris. */
-- Create a table that holds onto a subset of Polaris users
CREATE TABLE #TempPolarisLogins (
PolarisUserID INT,
CreationDate DATETIME,
Name NVARCHAR(50),
Library NVARCHAR(50),
Branch NVARCHAR(50)
);
-- Populate the table with users that meet our criteria
INSERT INTO #TempPolarisLogins
SELECT
pu.PolarisUserID,
pu.CreationDate,
pu.Name,
library.Name AS Library,
branch.Name AS Branch
FROM
Polaris.Polaris.PolarisUsers pu WITH (NOLOCK)
INNER JOIN -- Pull in the users' assigned library
Polaris.Polaris.Organizations library WITH (NOLOCK)
ON (pu.OrganizationID = library.OrganizationID)
LEFT JOIN -- Pull in the users' assigned branch
Polaris.Polaris.Organizations branch WITH (NOLOCK)
ON (pu.BranchID = branch.OrganizationID)
WHERE -- No need to deal with deleted users
pu.Name NOT LIKE '%deleted%'
AND -- Look for users in PolarisTransactions that haven't logged in for a while
pu.PolarisUserID NOT IN (
SELECT PolarisUserID
FROM PolarisTransactions.Polaris.TransactionHeaders WITH (NOLOCK)
WHERE TransactionTypeID = 7200 -- System logon
AND TranClientDate BETWEEN '2024-01-01 00:00:00.000' AND '2024-03-05 23:59:59.999' -- Adjust dates as desired
)
ORDER BY
pu.CreationDate DESC
-- Data delivery
SELECT
tpl.PolarisUserID AS [PolarisUserID],
tpl.CreationDate AS [Account Creation Date],
tpl.Name AS [Username],
tpl.Library AS [Assigned Library],
tpl.Branch AS [Assigned Branch],
MAX(th.TranClientDate) AS [Most Recent Login]
FROM
PolarisTransactions.Polaris.TransactionHeaders th WITH (NOLOCK)
INNER JOIN
#TempPolarisLogins tpl
ON (tpl.PolarisUserID = th.PolarisUserID)
GROUP BY
tpl.PolarisUserID,
tpl.CreationDate,
tpl.Name,
tpl.Library,
tpl.Branch
ORDER BY
tpl.CreationDate DESC;
-- Tidy up
DROP TABLE #TempPolarisLogins;