Solved

SQL Query

Posted on 2016-08-15
6
78 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 70

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 74

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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 74

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

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 …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
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 videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

685 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