Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
Medium Priority
?
59 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
[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 Comments
 
LVL 5

Accepted Solution

by:
Abhimanyu Suri earned 2000 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 49

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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

610 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