Currently I have written:
SELECT IT_ID, SUBSTR (SYS_CONNECT_BY_PATH (grp , ','), 2) GROUPS
FROM (SELECT U.IT_ID, LAST_NAME, FIRST_NAME, GRP, ROW_NUMBER() OVER (ORDER BY U.IT_ID) rn, COUNT(*) OVER() cnt
FROM ECG_IT_USERS U
JOIN SECUREGROUPS G ON U.IT_ID = G.IT_ID)
START WITH rn = 1
CONNECT BY rn = PRIOR rn + 1
This returns:
IT_ID GROUPS
afz23 ADMIN
afz23 ADMIN,QA
alv77 ADMIN,QA,USER
jaj23 ADMIN,QA,USER,USER
klo26 ADMIN,QA,USER,USER,PROD
klo26 ADMIN,QA,USER,USER,PROD,ADMIN
klo26 ADMIN,QA,USER,USER,PROD,ADMIN,QA
mav45 ADMIN,QA,USER,USER,PROD,ADMIN,QA,ADMIN
I can’t figure out how I can make it reset after a new user is encountered? It seems to be carrying over the previous groups, even if the user does not belong to them.
I need to see:
IT_ID GROUPS
afz23 ADMIN,QA
alv77 USER
jaj23 USER
klo26 PROD,ADMIN,QA
mav45 ADMIN
There are three things you need to do here.
Firstly you need to add a partition to the row_number function so that it starts the numbering from 1 for each IT_ID. You also need to add the IT_ID column to the connect by so that it only takes rows with the same
IT_IDvalue. Finally you need to group by the it_id column to remove the duplicate rows.The final query would be
This produces the following output for me:
EDIT: I have added a with clause with some sample data and this runs for me without any problems although I did comment out the last_name and first_name columns since they don’t have an effect on the final query and I put some brackets around the join condition.
Maybe it would pay to start with the query I’ve got above, check that it works for you initially and modify it as appropriate.