0

i want to manipulate query result

Original Query Result

Expected Result

original query is

SELECT grantee,
               privilege
          FROM DBA_TAB_PRIVS
         WHERE OWNER = 'BIZOWNFO'
           AND TABLE_NAME = 'TB_AT_CASHREC_DTLS';

I want to make result like this query

select grantee,
       (case when privilege = 'SELECT' then 'O' end ) as SEL,
       (case when privilege = 'UPDATE' then 'O' end ) as UPD,
       (case when privilege = 'INSERT' then 'O' end ) as INS,
       (case when privilege = 'DELETE' then 'O' end ) as DEL
FROM
(
SELECT grantee,
   privilege
FROM DBA_TAB_PRIVS
WHERE OWNER = 'BIZOWNFO'
AND TABLE_NAME = 'TB_AT_CASHREC_DTLS'
) group by grantee;

but it didn't work

i want to know how to do it.

2
  • Are you using MySQL, MS SQL Server or Oracle? (Remove the unrelated tags.) Commented Apr 23, 2021 at 7:35
  • It's the GROUP BY that causes the problem. Commented Apr 23, 2021 at 7:36

2 Answers 2

2

It looks like you need the GROUP BY clause with pivoting logic:

SELECT
    grantee AS "ROLE",
    MAX(CASE WHEN privilege = 'SELECT' THEN 'O' END) AS "SELECT",
    MAX(CASE WHEN privilege = 'UPDATE' THEN 'O' END) AS "UPDATE",
    MAX(CASE WHEN privilege = 'INSERT' THEN 'O' END) AS "INSERT",
    MAX(CASE WHEN privilege = 'DELETE' THEN 'O' END) AS "DELETE"
FROM DBA_TAB_PRIVS
WHERE
    OWNER = 'BIZOWNFO' AND TABLE_NAME = 'TB_AT_CASHREC_DTLS'
GROUP BY
    grantee;
Sign up to request clarification or add additional context in comments.

1 Comment

that is what i really want ! Thank you so much !
0

Looks like you should remove the GROUP BY clause:

SQL> SELECT grantee,
  2         (CASE WHEN privilege = 'SELECT' THEN 'O' END) AS sel,
  3         (CASE WHEN privilege = 'UPDATE' THEN 'O' END) AS upd,
  4         (CASE WHEN privilege = 'INSERT' THEN 'O' END) AS ins,
  5         (CASE WHEN privilege = 'DELETE' THEN 'O' END) AS del
  6    FROM (SELECT grantee, privilege
  7            FROM dba_tab_privs
  8           WHERE     owner = 'MIKE'
  9                 AND table_name = 'TEST');

GRANTEE                        S U I D
------------------------------ - - - -
KCDBA                          O
SCOTT                              O

SQL>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.