forked from folio-org/folio-analytics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers_groups.sql
100 lines (72 loc) · 3.32 KB
/
users_groups.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
DROP TABLE IF EXISTS users_groups;
-- Create a derived table that takes the user_users table and joins in
-- the group information. Does not include addresses - see additional
-- derived tables for addresses in different arrangements. Query also
-- depends on separate derived table for user departments
CREATE TABLE users_groups AS
WITH user_departments AS (
SELECT
user_id,
--string_agg(DISTINCT department_name, '|'::text) AS departments
string_agg(DISTINCT department_id, '|'::text) AS departments
FROM
users_departments_unpacked
GROUP BY
user_id
)
SELECT
uu.id AS user_id,
uu.active,
uu.barcode,
json_extract_path_text(uu.data, 'metadata', 'createdDate') AS created_date,
uu.enrollment_date,
uu.expiration_date,
json_extract_path_text(uu.data, 'externalSystemId') AS external_system_id,
uu.patron_group,
ug.desc AS group_description,
ug.group AS group_name,
ud.departments,
json_extract_path_text(uu.data, 'personal', 'lastName') AS user_last_name,
json_extract_path_text(uu.data, 'personal', 'firstName') AS user_first_name,
json_extract_path_text(uu.data, 'personal', 'middleName') AS user_middle_name,
json_extract_path_text(uu.data, 'personal', 'preferredFirstName') AS user_preferred_first_name,
json_extract_path_text(uu.data, 'personal', 'email') AS user_email,
json_extract_path_text(uu.data, 'personal', 'phone') AS user_phone,
json_extract_path_text(uu.data, 'personal', 'mobilePhone') AS user_mobile_phone,
json_extract_path_text(uu.data, 'personal', 'dateOfBirth') AS user_date_of_birth,
json_extract_path_text(uu.data, 'personal', 'preferredContactTypeId') AS user_preferred_contact_type_id,
json_extract_path_text(uu.data, 'type') AS user_type,
json_extract_path_text(uu.data, 'metadata', 'updatedDate') AS updated_date,
uu.username,
json_extract_path_text(uu.data, 'tags') AS user_tags,
json_extract_path_text(uu.data, 'customFields') AS user_custom_fields
FROM
user_users AS uu
LEFT JOIN user_groups AS ug ON uu.patron_group = ug.id
LEFT JOIN user_departments AS ud ON uu.id = ud.user_id;
CREATE INDEX ON users_groups (user_id);
CREATE INDEX ON users_groups (active);
CREATE INDEX ON users_groups (barcode);
CREATE INDEX ON users_groups (created_date);
CREATE INDEX ON users_groups (enrollment_date);
CREATE INDEX ON users_groups (expiration_date);
CREATE INDEX ON users_groups (external_system_id);
CREATE INDEX ON users_groups (patron_group);
CREATE INDEX ON users_groups (group_description);
CREATE INDEX ON users_groups (group_name);
CREATE INDEX ON users_groups (departments);
CREATE INDEX ON users_groups (user_last_name);
CREATE INDEX ON users_groups (user_first_name);
CREATE INDEX ON users_groups (user_middle_name);
CREATE INDEX ON users_groups (user_preferred_first_name);
CREATE INDEX ON users_groups (user_email);
CREATE INDEX ON users_groups (user_phone);
CREATE INDEX ON users_groups (user_mobile_phone);
CREATE INDEX ON users_groups (user_date_of_birth);
CREATE INDEX ON users_groups (user_preferred_contact_type_id);
CREATE INDEX ON users_groups (user_type);
CREATE INDEX ON users_groups (updated_date);
CREATE INDEX ON users_groups (username);
CREATE INDEX ON users_groups (user_tags);
CREATE INDEX ON users_groups (user_custom_fields);
VACUUM ANALYZE users_groups;