Solved

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

Posted on 2015-01-22
9
59 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 77

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 77

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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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
 

Author Comment

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

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 77

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
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 shows how to recover a database from a user managed backup
Suggested Courses

626 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