Grouping on a multiple columns in oracle 11g

Posted on 2015-02-19
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
  • 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.)
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

LVL 16

Accepted Solution

Swadhin Ray earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now