SQL Query

Posted on 2016-08-15
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');

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

Question by:SuperLight
LVL 70

Expert Comment

by:Éric Moreau
ID: 41756252

Author Comment

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

LVL 73

Expert Comment

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)
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 73

Accepted Solution

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.

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  /


Provided order of RULE doesn't matter

Author Closing Comment

ID: 41756707
Thanks v.much!

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

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server syntax question 13 32
T-SQL Query to include null values 3 30
Help with query 3 26
TSQL - How to declare table name 26 31
'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 …
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.

810 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