-1

I have two tables, ROLE and ROLE_DESC.

ROLE looks like this.

PS_ROLE_SEQ  NUMBER,    
PS_SEQ       NUMBER,    
ROLE_TYPE            VARCHAR2(2 CHAR),    
SOURCE               VARCHAR2(128 CHAR)

ROLE_DESC looks like this.

ROLE_TYPE  VARCHAR2(2 CHAR),    
ROLE_NAME  VARCHAR2(16 CHAR)

As you expect, ROLE_TYPE of ROLE and ROLE_TYPE of ROLE_DESC reference each other. There are three entities in ROLE_DESC, like this.

ROLE_TYPE   ROLE_NAME
A           Account
M           Manager
S           Sales

I would like to make this string for a particular PS_SEQ using where clause.

(Account + ":" + ROLE.SOURCE + "|" + Manager + ":" + ROLE.SOURCE + "|" + Sales + ":" + ROLE.SOURCE)

Sometimes, ROLE table does not have whole ROLE_TYPE, for example, it only has Account and Manager. In that case, ROLE.SOURCE of Sales become just "". And, ROLE_DESC is dynamic, the number of entities can be changed.

Is there any way I can query this easily?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Anna Lee
  • 909
  • 1
  • 17
  • 37

2 Answers2

0

You can try this:

select ROLE_DESC.ROLE_TYPE, ROLE_DESC.ROLE_NAME, ROLE.SOURCE 
FROM ROLE_DESC
LEFT OUTER JOIN ROLE
ON (ROLE_DESC.role_type=ROLE.role_type and ROLE.PS_SEQ = 111)
Aleksej
  • 22,443
  • 5
  • 33
  • 38
  • The role_desc table is dynamic, so I can't make it static. – Anna Lee May 04 '16 at 14:56
  • Please edit this question with the sample data you posted in the other one, then please close it, because it's a duplicate. After reading your second question, I edited y answer – Aleksej May 04 '16 at 14:58
0
SELECT LISTAGG (role_name || ':' || source, '|') WITHIN GROUP (ORDER BY role_name)
  FROM (SELECT ROLE_DESC.ROLE_TYPE, ROLE_DESC.ROLE_NAME, ROLE.SOURCE
          FROM ROLE_DESC 
           LEFT OUTER JOIN ROLE 
           ON ROLE_DESC.role_type = ROLE.role_type AND ROLE.PS_SEQ = 111)
Mottor
  • 1,938
  • 3
  • 12
  • 29