使用SqL报告

时间:2015-04-02 00:17:07

标签: sql oracle

想要使用具有确切输出的3个表创建报告

用户表

User id  User name 
1         user1
2         user2
3         user3  

USER_IN_ROLES表

USRINR_USER_ID   USRINR_ROLES_ID

1                        103
3                        104
2                        105
1                        107
2                        108
1                        105
1                        108

角色rl

Roles ID                 Description
104                      User management
107                      Modify
103                      Debit
105                      General
108                      Audit

表1和表1之间的关系用户ID 2和角色ID介于2和2之间3

我想要一个像这样的操作的查询

UName   FirstEntitl     Second Entitl
user1   Read only user  Debit
user3   Security admin  User Management
user2    Team           General
user1   Read only user  Modify
user2    Team           Audit
user1   Read only user  General
user1   Read only user  Audit

我的尝试非常基本

 select usr.user_name, rl.role_code as firstentitl,
 Case
   When rl.role_code = 'UserManagement' then 'Sec Admin'
   When rl.role_code  = 'ManualModification' then 'BelgiumTax User'
   End as secondEntitileme
from users usr, USER_IN_ROLES uir,  roles rl
where usr.user_id=uir.usinr_user_id 
and rl.role_id=uir.usinr_role_id;

因此,所有具有用户管理角色(没有其他角色)的用户都将成为安全管理员具有身份角色的用户将成为团队,如果用户没有修改且用户managemnt是只读用户

1 个答案:

答案 0 :(得分:0)

rl.role_code as firstentitl,

表中没有这样的列,如果你想要那种输出,那么需要在用户角色中维护它,并将userid映射到sql中的角色和查询。

SCOTT@research 02-APR-15> select * from user1;

     USRID UNAME      ROLEDET
---------- ---------- ----------------------------------------------------------------------------
         1 user1      Read only user
         2 user2      Team
         3 user3      Security admin

SCOTT@research 02-APR-15> select * from usr_roles;

     USRID     ROLEID
---------- ----------
         1        103
         2        104
         3        105

SCOTT@research 02-APR-15> select * from role_det ;

    ROLEID ROLEDET
---------- ---------------------------------------------------------------------------------------
       103 user management
       104 modify
       105 debt

SCOTT@research 02-APR-15>  select usr.USRID, usr.roledet as firstentitl,
  2   Case
  3     When rl.roledet = 'user management' then 'Sec Admin'
  4     When rl.roledet  = 'modify' then 'BelgiumTax User'
  5     End as secondEntitileme
  6  from user1 usr, usr_roles uir,  role_det rl
  7  where usr.USRID=uir.USRID
  8  and rl.ROLEID=uir.ROLEID;

UNAME      FIRSTENTITL                              SECONDENTITILEM
---------- ---------------------------------------- ---------------
user1      Read only user                           Sec Admin
user2      Team                                     BelgiumTax User
user3      Security admin