I’ve found that query really helpful for our site, but it took a while to customise / hack, make it work by group, and include some other user stuff like emails.
However, I’m about to embark on a large instance with some user fields which will be a bit dynamic for a while.
Does anyone out there have the smarts to re-build this query so that it does automatically spit out all user data fields? Maybe @simon?
Here is my query at the mo:
–[params]
– string :group_nameWith included_users AS (
SELECT
gu.user_id
FROM group_users gu
JOIN groups g
ON g.id = gu.group_id
WHERE g.name = :group_name
),
e AS (
SELECT email, user_id
FROM user_emails u
WHERE u.primary = true
)SELECT
u.id AS user_id, u.username, u.name, e.email,
MAX(CASE ucf.name WHEN ‘user_field_6’ THEN value END) AS “Mobile Phone”,
MAX(CASE ucf.name WHEN ‘user_field_2’ THEN value END) AS “Job Title”,
MAX(CASE ucf.name WHEN ‘user_field_5’ THEN value END) AS “Employer”,
MAX(CASE ucf.name WHEN ‘user_field_4’ THEN value END) AS “Postcode”,
MAX(CASE ucf.name WHEN ‘user_field_14’ THEN value END) AS “Clinical Profession”,
MAX(CASE ucf.name WHEN ‘user_field_3’ THEN value END) AS “Clinical Speciality”,
MAX(CASE ucf.name WHEN ‘user_field_17’ THEN value END) AS “Working Clinically”,
MAX(CASE ucf.name WHEN ‘user_field_1’ THEN value END) AS “Informatics Interest”,
MAX(CASE ucf.name WHEN ‘user_field_15’ THEN value END) AS “Dietary Requirement”FROM user_custom_fields AS ucf
RIGHT JOIN users AS U on ucf.user_id = u.id
RIGHT JOIN e on ucf.user_id = e.user_id
WHERE u.id IN (SELECT user_id FROM included_users)
GROUP BY u.id, e.email
ORDER BY username asc