SELECT max(aaauser.first_name) AS "Name"
,max(aaalogin.name) AS "Login Name"
,max(aaacontactinfo.emailid) AS "Primary Email"
,max(departmentdefinition.deptname) AS "Department Name"
,max(sdorganization.NAME) AS "Site"
,max(sduser.jobtitle) AS "Job title"
,max(sdus.first_name) AS "Reporting to"
,max(aaalogin.domainname) AS "Domain Name"
,"Assigned Roles" = STUFF((
SELECT CHAR(10) + aar.name
FROM aaauser a
LEFT JOIN aaalogin al
ON al.user_id = a.user_id
LEFT JOIN aaaaccount aaac
ON al.login_id = aaac.login_id
LEFT JOIN SDAuthorizedRole sdr
ON aaac.account_id = sdr.account_id
LEFT JOIN aaarole aar
ON aar.role_id = sdr.role_id
WHERE aaauser.user_id = a.user_id for xml path ('')), 1, 1, '')
FROM aaauser
LEFT JOIN sduser
ON aaauser.user_id=sduser.userid
LEFT JOIN aaauser sdus
on sduser.REPORTINGTO = sdus.USER_ID
LEFT JOIN aaalogin
ON aaalogin.user_id = aaauser.user_id
LEFT JOIN aaaaccount
ON aaalogin.login_id = aaaaccount.login_id
LEFT JOIN portaltechnicians
ON aaauser.user_id=portaltechnicians.userid
LEFT JOIN userdepartment
ON aaauser.user_id=userdepartment.userid
LEFT JOIN departmentdefinition
ON userdepartment.deptid=departmentdefinition.deptid
LEFT JOIN sdorganization
ON departmentdefinition.siteid = sdorganization.org_id
LEFT JOIN aaausercontactinfo
ON aaausercontactinfo.user_id=aaauser.user_id
LEFT JOIN aaacontactinfo
ON aaausercontactinfo.contactinfo_id=aaacontactinfo.contactinfo_id
WHERE sduser.status='ACTIVE'
AND portaltechnicians.userid is not null
GROUP BY aaauser.user_id;