Question

What is the logic I am missing to eliminate duplicate values from listagg function in following script.?

The following the sys.privs table:

SQL> desc sys.privs
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 GRANTEE                                            VARCHAR2(30 CHAR)
 GRANTED_ROLE                              NOT NULL VARCHAR2(30)
 PRIVILEGE                                 NOT NULL VARCHAR2(40)
 OWNER                                     NOT NULL VARCHAR2(30)
 TABLE_NAME                                NOT NULL VARCHAR2(30)

The following is the script

SELECT grantee,
     LISTAGG(granted_role, '-') WITHIN GROUP (ORDER BY granted_role) AS granted_role,
     LISTAGG(privilege, '-') WITHIN GROUP (ORDER BY privilege) AS privs,
     owner,
     table_name
FROM (
     SELECT UNIQUE
            grantee,
            granted_role,
            privilege,
            owner,
            table_name
       FROM sys.privs
     )
GROUP BY grantee, owner, table_name;
Was it helpful?

Solution

You can do this by taking advantage of the fact the listagg() ignores NULL values. So, use row_number() to enumerate duplicates and then just pull in the first value:

SELECT grantee,
       LISTAGG(CASE WHEN seqnum_granted_role = 1 THEN granted_role END, '-')
           WITHIN GROUP (ORDER BY granted_role) AS granted_role,
       LISTAGG(CASE WHEN seqnum_privilege = 1 THEN privilege END, '-')
           WITHIN GROUP (ORDER BY privilege) AS privs,
       owner,
       table_name
FROM (SELECT grantee, granted_role, privilege,
             owner, table_name,
             ROW_NUMBER() OVER (PARTITION BY grantee, owner, table_name, granted_role
                                ORDER BY grantee
                               ) as seqnum_granted_role,
             ROW_NUMBER() OVER (PARTITION BY grantee, owner, table_name, privilege
                                ORDER BY grantee
                               ) as seqnum_privilege
       FROM sys.privs
     ) p
GROUP BY grantee, owner, table_name;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top