I am trying to get a listing of all the users in an SQL Anywhere 17 database along with the roles they have been granted. The trick is that I want the list of roles in a single field in the query. My first attempt was something like this:
select u.user_id, u.user_name, p.login_policy_name, u.last_login_time, list(g.user_name,',' order by g.user_name)
from sysuser u join
sysloginpolicy p on (u.login_policy_id = p.login_policy_id) left JOIN
sysgroup ug on (u.user_id = ug.group_member) left join
sysuser g on (ug.group_id = g.user_id)
where u.user_type = 12 and u.user_id = 138
order by u.user_name
This gave me the error "Function or column reference to 'user_id' must also appear in a GROUP BY". So I then tried a sub-query:
select u.user_id, u.user_name, p.login_policy_name, u.last_login_time,
(select list(u.user_name, ',' order by u.user_name)
from sysgroup g left join sysuser u on (g.group_id = u.user_id)
where g.group_member = u.user_id) groups
from sysuser u join
sysloginpolicy p on (u.login_policy_id = p.login_policy_id)
where u.user_type = 12
order by u.user_name
This didn't give me an error but also didn't return any data in the groups column.
If I run just the subquery with a valid user_id as follows, I do get the correct data:
select list(u.user_name, ',' order by u.user_name)
from sysgroup g left join sysuser u on (g.group_id = u.user_id)
where g.group_member = 158
I don't have a lot of experience with SQL Anywhere so what am I missing?