Grouping on a multiple columns in oracle 11g

Posted on 2015-02-19
Medium Priority
Last Modified: 2015-02-20
I have attached an example document that shows of an output I would like to see.
Here is the query that I've been trying to use.  Is this where grouping can come in handy
or any other statements that can be used.

SELECT act.act_first_name || ' ' || acr1.act_last_name "Actor",
  acr1.rol_code "Role",
  arg.arg_rgp_code "Rights Group",
  act.act_manager_code "Manager"
FROM v_act_actor act
LEFT JOIN v_acr_actor_role_1 acr1 ON act.act_id = acr1.acr_act_id
LEFT JOIN v_arg_act_rightgroup arg ON act.act_id = arg.arg_act_id
WHERE act.act_status     = 'A'
AND act.act_manager_code = '49364'
Question by:Machinegunner
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
LVL 16

Expert Comment

by:Swadhin Ray
ID: 40620717
Can you give some sample data with table structure ?

Author Comment

ID: 40621086
Thanks for the reply,
Sample data attached.
Table structure are that all the columns are varchar2(x) as the datatypes.
(Actor                varchar2(50),
  Role                  varchar2(250),
  Rights_Group  varchar2(250),
  Manager          varchar2(40))

Let's go with a simpler select without any joins like below:

FROM v_act_actor act
WHERE act_status  = 'A'
AND act_manager_code = '49364'
LVL 35

Expert Comment

by:Mark Geerlings
ID: 40621177
So, you want multiple lines of output (if the Actor has multiple Roles or Rights_Group records)  but you want the Actor's name to print only on the first line of output for that Actor, correct?

If you run your query in SQL*Plus, use this command first:
break on actor

Also, you should add an "order by" clause, something like this:
order by actor, role, rights_group

(Remember that in Oracle queries, if you don't add an explicit "order by" clause, the order that the records are displayed to you in will usually not match what you want or expect.)

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

LVL 16

Accepted Solution

Swadhin Ray earned 2000 total points
ID: 40621243
Here is the sample data and table structure I created , assume that the datatype can be changed but the data are ok to test :

create table v_act_actor
act_first_name varchar2(100)
,act_manager_code number 
,act_status varchar2(1)
,act_id number 
) ;

create table v_acr_actor_role_1
act_last_name varchar2(100)
,rol_code varchar2(100)
,acr_act_id number 
) ; 

create table v_arg_act_rightgroup
arg_rgp_code varchar2(100)
,arg_act_id number 

insert into v_act_actor values('Terry',49364,'A', 1  ) ; 
insert into v_act_actor values('Steven',49364,'A', 2  ) ; 
insert into v_acr_actor_role_1 values('Burns', 'INTERNAL ACTOR',1 );
insert into v_acr_actor_role_1 values('Burns', 'QE',1 );
insert into v_acr_actor_role_1 values('Burns', 'DE',1 );
insert into v_acr_actor_role_1 values('Wallaby', 'INTERNAL ACTOR',2 );
insert into v_acr_actor_role_1 values('Wallaby', 'QE',2 );
insert into v_acr_actor_role_1 values('Wallaby', 'QA',2 );
insert into v_arg_act_rightgroup values ('QE', 1) ; 
insert into v_arg_act_rightgroup values ('UNIVERSAL', 1) ; 
insert into v_arg_act_rightgroup values ('DE', 1) ; 
insert into v_arg_act_rightgroup values ('QA', 2) ; 
insert into v_arg_act_rightgroup values ('QE', 2) ; 
insert into v_arg_act_rightgroup values ('DE', 2) ; 

Open in new window

Here is the SQL:

create or replace view grp_v as 
with t1 as
(select a.act_first_name || b.act_last_name as Actor,
row_number() over (partition by b.act_last_name order by a.act_first_name )as rno
from v_act_actor a, v_acr_actor_role_1 b 
where a.act_id = b.acr_act_id),
t2 as
(select c.arg_rgp_code,c.arg_act_id,
row_number() over (partition by c.arg_act_id order by c.arg_act_id)as rno
from v_arg_act_rightgroup c)
select t1.Actor ,
       t1.rol_code ,
  from t1,t2
  where t1.rno=t2.rno
  and t1.acr_act_id = t2.arg_act_id;

  select case row_number() over(partition by Actor
   order by rol_code
   )  when 1 then Actor
             else null
end Actor ,
rol_code as "Role", 
arg_rgp_code as "Rights_Group",
act_manager_code as "Manager"
      from grp_v ; 

Open in new window


Author Comment

ID: 40621244
Thanks for the reply.
Yes, I would like it to break on Actor and I did try the 'break on' command but would like something
similar to work within either SqlDeveloper or Toad.

Can this be done within these apps other than sqlplus?
LVL 16

Expert Comment

by:Swadhin Ray
ID: 40621289
Did you checked the SQL which I have provided.

Author Closing Comment

ID: 40621373
Thanks!   I created a temp table instead as didn't have permissions to create a view using the account that I was provided.
I was able to run afterwards.
Thanks again,

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question