Solved

SQL Query

Posted on 2016-08-15
6
68 Views
Last Modified: 2016-08-15
Hi All,

I'm time-pressured and stuck on a simple query.

Sample data:

drop table tab1 purge;
create table tab1(COL_A varchar2(50), COL_B varchar2(50), COL_C varchar2(50), R_NAME varchar2(10));

insert into tab1 values('X','Y','Z','RULE1');
insert into tab1 values('X','Y','Z','RULE2');
insert into tab1 values('X','Z','Z','RULE1');
insert into tab1 values('Y','Y','Z','RULE2');
insert into tab1 values('A','B','C','RULE3');
insert into tab1 values('A','B','C','RULE4');
insert into tab1 values('A','B','C','RULE1');
commit;

Open in new window


I need to return on the rows the distinct values of COL_A, COL_B & COL_V and return R_NAME in multiple Columns.

Therefore the output should look like this:

X | Y | Z | RULE1 | RULE2
X | Z | Z | RULE1
Y | Y | Z | RULE2
A | B | C | RULE3 | RULE4 | RULE1

Thanks,
Mark
0
Comment
Question by:SuperLight
6 Comments
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 41756252
0
 

Author Comment

by:SuperLight
ID: 41756293
In my haste I should have made it clear that this is for an Oracle Database.

thanks,
Mark
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 41756341
you can't have a dynamic column list with a sql statement.

The columns to be returned must be known at the time the query is parsed (i.e. before it executes)
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 73

Accepted Solution

by:
sdstuber earned 350 total points
ID: 41756346
you can aggregate in oracle with the listagg function

select col_a,col_b,col_c,listagg(r_name,',') within group(order by r_name)
from tab1
group by col_a,col_b,col_c

but that puts them all into a single column.  if you want multiple columns, you must define what columns they will be and, since each row will have a variable number of results,  you must define how the columns should be populated since not every row will have enough values to fill the columns.
0
 
LVL 4

Assisted Solution

by:Abhimanyu Suri
Abhimanyu Suri earned 150 total points
ID: 41756397
SQL> select COL_A||'|'||COL_B||'|'||COL_C||'|'||listagg(R_NAME,'|') within group (order by COL_A,COL_B,COL_C) RESULT
  2  from asuri_admin.tab1
  3  group by COL_A,COL_B,COL_C
  4  /

RESULT
------------------------------------------------------------------------------------------------------------------------------------------------------
A|B|C|RULE1|RULE3|RULE4
X|Y|Z|RULE1|RULE2
X|Z|Z|RULE1
Y|Y|Z|RULE2

Provided order of RULE doesn't matter
0
 

Author Closing Comment

by:SuperLight
ID: 41756707
Thanks v.much!

(so much too learn and so little time ;o))
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows how to recover a database from a user managed backup

743 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

12 Experts available now in Live!

Get 1:1 Help Now