Q:

How to use stuff() for listing user roles per application? 

Consider the following table users
application  user_id    user_role     role_permissions
-------------------------------------------------------
1            20          A             add
1            20          B             edit
1            30          A             add
1            20          B             edit
1            30          V             delete
1            20          G             duplicate
1            40          X             purge
2            20          W             hide
2            20          P             unhide
2            40          X             purge

I want to list all the roles that each user has per application, as follows:
application  user_id    roles       permissions
-----------------------------------------------
1            20         A;B;G       add;edit;duplicate
1            30         A;V         add;delete
1            40         X           purge
2            20         W;P         hide;unhide
2            40         X           purge

I have been trying this using stuff but I am unsuccessful:
select users.user_id,
       stuff
       ((
           select ';' + user_role from users a
           where  a.user_id = b.user_id
           for xml path('')),1,1,'' 
       ) as roles
 from users b

This gives me:
user_id  permissions
-----------------------
20       A;B;B;G
20       A;B;B;G
20       A;B;B;G
30       A;V
30       A;V
...
etc

How can I get the distinct rows PER application? 

A:

Use distinct before the outer select:
select u.user_id,
       stuff( (select distinct';' + user_role
               from users u2
               where u.user_id = u2.user_id and u.application = u2.application
               for xml path('')
              ), 1, 1, '' 
            ) as roles
from (select distinct user_id, application from users u) u;

I don't know what obj_id is.   The obvious correlation condition uses user_id.