ID_ROLE NAME_OF_ROLE
---------- -------------
1 Manager
2 Director
4 Supervisor
8 Secretary
16 Controlor
...
2048 Boss
这是我的ORACLE表。正如你所看到的,列ID_ROLE是2的幂。第一个角色是0到0的幂,第二个角色是2到1的幂,最后一个角色是2到11的幂,等等。这很容易当有人只有一个角色,我可以制作CASE或DECODE功能。例如,如果ID_ROLE是2,则该人是“Director”。但问题是当某人有两个或更多角色时。如果ID_ROLE是24(例如),则表示该人具有角色“秘书”和“控制器”(8 + 16),并且它应该返回类似“秘书/控制器”的文本。
知道怎么做吗?
答案 0 :(得分:2)
这是使用
的解决方案SELECT LISTAGG(NAME_OF_ROLE, ' / ')
WITHIN GROUP (ORDER BY ID_ROLE) "ROLE_NAMES"
FROM ROLES
WHERE BITAND(ID_ROLE, 24) = ID_ROLE;
这导致输出:
+------------------------+
| ROLE_NAMES |
+------------------------+
| Secretary / Controller |
+------------------------+
我使用的DDL:
CREATE TABLE "ROLES" (
"ID_ROLE" NUMBER NOT NULL ENABLE,
"NAME_OF_ROLE" VARCHAR2(4000),
CONSTRAINT "ROLES_PK" PRIMARY KEY ("ID_ROLE") USING INDEX ENABLE
);
我的测试数据:
INSERT INTO ROLES (NAME_OF_ROLE, ID_ROLE) VALUES ('Manager', 1);
INSERT INTO ROLES (NAME_OF_ROLE, ID_ROLE) VALUES ('Director', 2);
INSERT INTO ROLES (NAME_OF_ROLE, ID_ROLE) VALUES ('Supervisor', 4);
INSERT INTO ROLES (NAME_OF_ROLE, ID_ROLE) VALUES ('Secretary', 8);
INSERT INTO ROLES (NAME_OF_ROLE, ID_ROLE) VALUES ('Controller', 16);
INSERT INTO ROLES (NAME_OF_ROLE, ID_ROLE) VALUES ('Boss', 2048);