Solved

SQL Query

Posted on 2016-08-15
6
71 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

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

Suggested Solutions

Title # Comments Views Activity
Export table into csv file in oracle 10 46
'G_F01' is not a procedure or is undefined 3 12
My Query is not giving correct result. Please help 5 30
Oracle - SQL Parse String 5 20
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
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

895 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

17 Experts available now in Live!

Get 1:1 Help Now