Solved

how to write case statement or loop statement in simple sql query for same product with multiple sub products??

Posted on 2016-09-01
4
27 Views
Last Modified: 2016-10-22
how to write case statement or loop statement in simple sql query for same product with multiple sub products??


eg If I have product computer then sub products are like laptop,desktop,harddisk,ram etc

table is like productno | productsubno | productdesc|
                         1                         2                              laptop
                          1                          2                           desktop
                         1                         2                            harddisk                     like this...

and I want output like  

product            productsubcode   desc1       desc2          desc3            etc...
1                                         2             laptop        desktop       hardisk      


Please help in simple sql query...
0
Comment
Question by:newdeveloper india
4 Comments
 
LVL 4

Accepted Solution

by:
Abhimanyu Suri earned 500 total points
ID: 41779802
SELECT productno,
         productsubno,
         LISTAGG (productdesc, ',') WITHIN GROUP (ORDER BY productdesc)
    FROM TABLE_X
GROUP BY productno, productsubno

o/p :

1 2 laptop,desktop,hardisk

You can achieve output without commas by modifying
" LISTAGG (productdesc, ',') WITHIN GROUP (ORDER BY productdesc)"  TO
" LISTAGG (productdesc, '      ') WITHIN GROUP (ORDER BY productdesc)"

You can publish it as a csv by using

  SELECT productno||','||
         productsubno||','||
         LISTAGG (productdesc, ',') WITHIN GROUP (ORDER BY productdesc)
    FROM TABLE_X
GROUP BY productno, productsubno

1,2,laptop,desktop,hardisk
0
 

Author Comment

by:newdeveloper india
ID: 41785837
error found like this..

 FROM keyword not found where expected
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 41785860
which query did you use? did you transcribe it accurately?

i.e. it is hard for use to know what that error relates to. Please copy/paste the EXACT query you used and the EXACT error message.
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

Suggested Solutions

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…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

760 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

20 Experts available now in Live!

Get 1:1 Help Now