-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathExaminePatronItemCrossBorrowing.sql
174 lines (153 loc) · 5.66 KB
/
ExaminePatronItemCrossBorrowing.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
-- Thanks to Daniel Messer
/* In our case, the state report only cares about the number of items you lend/borrow, not how many
checkouts occurred. */
-- This is the master temp table used to collect and deliver data.
CREATE TABLE #TempCircLendingData (
ItemOrgName NVARCHAR(50) NULL,
ItemOrgID INT NULL,
MaterialTypeID INT NULL,
MatDescription NVARCHAR(50) NULL,
TransactionDate DATETIME,
TransactOrgName NVARCHAR(50) NULL,
TransactionTypeID INT,
ItemRecordID INT,
PatronID INT,
TransactionID INT
);
-- Begin populating that table
INSERT INTO #TempCircLendingData
SELECT
iab.Name,
itemab.numValue,
0, -- Initializing - will update later with MaterialTypeID
0, -- Initializing - will update later with MaterialType Description
th.TranClientDate,
torg.Name,
th.TransactionTypeID,
itemid.numValue,
0, -- Initializing - will update later with PatronID
th.TransactionID
FROM
PolarisTransactions.Polaris.TransactionHeaders th WITH (NOLOCK)
INNER JOIN -- Item assigned branch
PolarisTransactions.Polaris.TransactionDetails itemab WITH (NOLOCK)
ON (th.TransactionID = itemab.TransactionID AND itemab.TransactionSubTypeID = 125)
INNER JOIN -- Item assigned branch readable name
Polaris.Polaris.Organizations iab WITH (NOLOCK)
ON (iab.OrganizationID = itemab.numValue)
INNER JOIN -- ItemRecordID
PolarisTransactions.Polaris.TransactionDetails itemid WITH (NOLOCK)
ON (th.TransactionID = itemid.TransactionID AND itemid.TransactionSubTypeID = 38)
INNER JOIN -- Transacting branch readable name
Polaris.Polaris.Organizations torg WITH (NOLOCK)
ON (th.OrganizationID = torg.OrganizationID)
WHERE -- Check out
th.TransactionTypeID = 6001
AND -- Adjust dates as needed
th.TranClientDate BETWEEN '2023-07-01 00:00:00.000' AND '2024-06-30 23:59:59.999'
AND -- Put in the OrganizationIDs for the item's assigned branches and set boolean operations as desired
itemab.numValue IN (14,15,110,113)
AND -- Put in the OrganizatonsIDs for the transacting branches and set boolean operations as desired
th.OrganizationID NOT IN (14,15,110,113)
-- DEBUG
--SELECT * FROM #TempCircLendingData;
/* ---------------------------------------------------------------------------------------- */
/* If you do too much and try to pull in too much data for the table above, this query can go from an
execution time measured in seconds to an execution time measured in minutes. So we'll bring in the
MaterialType data separately, acting upon the TransactionIDs in #TempCircLendingData, and then update
the #TempCircLendingData table accordingly. */
CREATE TABLE #TempMaterialData (
MaterialTypeID INT,
MatDescription NVARCHAR(50),
TransactionID INT
);
INSERT INTO
#TempMaterialData
SELECT
material.numValue,
mat.Description,
th.TransactionID
FROM
PolarisTransactions.Polaris.TransactionHeaders th WITH (NOLOCK)
INNER JOIN -- Get MaterialTypeID
PolarisTransactions.Polaris.TransactionDetails material WITH (NOLOCK)
ON (th.TransactionID = material.TransactionID AND material.TransactionSubTypeID = 4)
INNER JOIN -- Get readable MaterialType description
Polaris.Polaris.MaterialTypes mat WITH (NOLOCK)
ON (mat.MaterialTypeID = material.numValue)
WHERE
th.TransactionID IN (
SELECT TransactionID FROM #TempCircLendingData
);
-- DEBUG
--SELECT * FROM #TempMaterialData;
/* ---------------------------------------------------------------------------------------- */
/* If you do too much and try to pull in too much data for the table above, this query can go from an
execution time measured in seconds to an execution time measured in minutes. So we'll bring in the
PatronID data separately, acting upon the TransactionIDs in #TempCircLendingData, and then update
the #TempCircLendingData table accordingly. */
CREATE TABLE #TempPatronData (
PatronID INT,
TransactionID INT
);
INSERT INTO
#TempPatronData
SELECT
patron.numValue,
th.TransactionID
FROM
PolarisTransactions.Polaris.TransactionHeaders th WITH (NOLOCK)
INNER JOIN -- Get PatronID
PolarisTransactions.Polaris.TransactionDetails patron WITH (NOLOCK)
ON (th.TransactionID = patron.TransactionID AND patron.TransactionSubTypeID = 6)
WHERE
th.TransactionID IN (
SELECT TransactionID FROM #TempCircLendingData
);
/* ---------------------------------------------------------------------------------------- */
/* Update the #TempCircLendingData table with MaterialType and Patron data */
-- Update MaterialTypes
UPDATE
#TempCircLendingData
SET
#TempCircLendingData.MaterialTypeID = #TempMaterialData.MaterialTypeID,
#TempCircLendingData.MatDescription = #TempMaterialData.MatDescription
FROM
#TempMaterialData
WHERE
#TempMaterialData.TransactionID = #TempCircLendingData.TransactionID;
-- Update Patron
UPDATE
#TempCircLendingData
SET
#TempCircLendingData.PatronID = #TempPatronData.PatronID
FROM
#TempPatronData
WHERE
#TempPatronData.TransactionID = #TempCircLendingData.TransactionID;
/* ---------- DATA DELIVERY ---------- */
/* The #TempCircLendingData table is fully populated. Put your final data delivery queries below. */
/*SELECT
ItemOrgName AS [Item Assigned Library],
ItemOrgID AS [Item Assigned OrgID],
MaterialTypeID,
MatDescription AS [Material Type],
TransactionDate,
TransactOrgName AS [Checkout Library],
PatronID,
ItemRecordID
FROM
#TempCircLendingData
WHERE
MaterialTypeID NOT IN (38,39,45,157,161,162,163,164);*/
SELECT
PatronID,
COUNT(DISTINCT ItemRecordID)
FROM
#TempCircLendingData
GROUP BY
PatronID;
-- Tidy up
DROP TABLE #TempCircLendingData;
DROP TABLE #TempMaterialData;
DROP TABLE #TempPatronData;