Solved

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

Posted on 2015-01-22
9
56 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

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…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

751 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