Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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.)
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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…
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

597 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