Solved

Oracle SQL: Rolling up by data by Primary Key SQL syntax in SQL developer

Posted on 2015-01-22
9
46 Views
Last Modified: 2016-06-18
Here is the output (pipe delimited) I am looking for (the first row is a header)

Order Number|Book Names
100|[Book1, Book12, Book11]
200|[Book9, Book1]
300|[Book3]
400|[Book11, Book10, Book2, Book4]


Where 'Order Number' is the first column in tab1 table.
The second column 'Book Names' is a roll-up of all the book names for that order in brackets []

Sample table data:
/*
drop table tab1 purge;
create table tab1(ordNum number, line number, InventoryID number);

insert into tab1 values(100,1,100001);
insert into tab1 values(100,2,100012);
insert into tab1 values(100,3,100011);
insert into tab1 values(200,1,100009);
insert into tab1 values(200,2,100001);
insert into tab1 values(300,1,100003);
insert into tab1 values(400,1,100011);
insert into tab1 values(400,2,100010);
insert into tab1 values(400,3,100002);
insert into tab1 values(400,4,100004);


drop table tab2 purge;
create table tab2(InventoryID number, InventoryName varchar2(20));


insert into tab2 values(100001,'Book1');
insert into tab2 values(100002,'Book2');
insert into tab2 values(100003,'Book3');
insert into tab2 values(100004,'Book4');
insert into tab2 values(100005,'Book5');
insert into tab2 values(100006,'Book6');
insert into tab2 values(100007,'Book7');
insert into tab2 values(100008,'Book8');
insert into tab2 values(100009,'Book9');
insert into tab2 values(100010,'Book10');
insert into tab2 values(100011,'Book11');
insert into tab2 values(100012,'Book12');
commit;
*/
0
Comment
Question by:Newbie345
  • 4
  • 4
9 Comments
 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 40564452
Based on your previous questions, you might have problems here since this also uses SPOOL and listagg.

What is your database version?

set pages 0
set lines 10000
set feedback off
spool myfile.txt
prompt Order Number|Book Names
select ordnum || '|[' || listagg(InventoryName,',') within group(order by t1.inventoryid) || ']'
from tab1 t1 join tab2 t2 on t1.inventoryid=t2.inventoryid
group by ordnum
/
spool off

Open in new window

0
 
LVL 76

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 500 total points
ID: 40564462
If you cannot use LISTAGG, here is an XML version:
set pages 0
set lines 10000
set feedback off
spool myfile.txt
prompt Order Number|Book Names
select ordnum || '|[' ||
RTRIM(
           EXTRACT(XMLAGG(XMLELEMENT("s", t2.inventoryname || ',')), '/s/text()').getstringval(),
           ','
       )
|| ']'
from tab1 t1 join tab2 t2 on t1.inventoryid=t2.inventoryid
group by ordnum
/
spool off

Open in new window

0
 

Author Comment

by:Newbie345
ID: 40564530
Thanks slightwv. I will be using SSIS for routing the output from the SQL query to a text file so spool might no longer be an issue here. I am using Oracle SQL developer 2.1.1.64. I will test this syntax sometime later today and will get back to you tomorrow. Thanks again for the prompt help. I appreciate it.
0
 

Author Comment

by:Newbie345
ID: 40567846
Slightwv,

For some reason, I am getting the following error

ORA-00923: FROM keyword not found where expected
00923 00000 - "FROM keyword not found where expected"
*Cause
*Action:

Can you send the query that I can run in the SQL developer directly (not from SQL PLUS)
0
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.

 

Author Comment

by:Newbie345
ID: 40567855
Is it possible to achieve this using a while loop?
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40568644
>>For some reason, I am getting the following error

Execute it as a script.

>>Is it possible to achieve this using a while loop?

Possibly but what would the result be returned as if you use PL/SQL?

If you want the file on the database server you can create a stored procedure and use UTL_FILE to write the data to a file.  There are TONs of examples of UTL_FILE.  Jsut use the query I provided in a loop.

>>I will be using SSIS for routing the output from the SQL query to a text file

I think you mentioned this in your last question.  Why would you want to use a non-Oracle product to do something that Oracle will easily do out of the box?
0
 

Author Comment

by:Newbie345
ID: 40568853
I am not sure about the background but there have been several queries scheduled through SSIS already. So I was asked to develop this query using SSIS package as well.

Is there a way to just get the SQL query for this (rather than developing a stored procedure which I am not allowed to)?
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40569806
I gave you the SQL query.  I actually gave you two.  One using LISTAGG and one using an XML trick.
0

Featured Post

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.

Join & Write a Comment

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

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