Solved

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

Posted on 2015-01-22
9
53 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
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: 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 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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
scheduler for Procedure in DB with 3 arguments in 10g 7 34
SQL query question 8 78
upgrading Oracle 10g/ 11g / 11g R2 to Oracle 12c 25 54
Oracle dataguard 5 31
Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
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…
Via a live example, show how to take different types of Oracle backups using RMAN.

778 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