show the sub-query in one line?

Posted on 2011-02-25
Medium Priority
Last Modified: 2012-05-11
Hi everyone;

I apologize for the bad english.

I have a two tables;


pid   name  code
1      aaa     x25
2      bbb     x26

2. Table

tid   pid    total_reserve
1     1      10
2     1      10
3     1      2
4     2      12
5     2       8

I want my sql output;
pid     name    code    total_reserve
1        aaa       x25      10,10,2
2        bbb       x26      12,8


pid     name    code    total_reserve
1        aaa       x25      10                               ## row 1
2        bbb       x26      12                               ## row 2

Can I show the sub-query in one line?

Thank you for advice and assistance.

Question by:dmiebim
LVL 32

Accepted Solution

Ephraim Wangoya earned 2000 total points
ID: 34983916
try this

select pid, name, code,  (SELECT  
                                    SELECT ', ' + total_reserve
                                    FROM Table2
                                                where Table2.pid = Table1.pid
                                    FOR XML PATH ('')  
                              )) as total_reserve
from Table1
LVL 26

Expert Comment

ID: 34984027
     select t2.pid, tx.name, tx.code,  STUFF(
            select ','+ cast(total_reserve as varchar)
             from #table2 t1
             where t1.pid = t2.pid
             for XML path ('')),1,1,'') as  totalReserves
      from #table2 t2
            inner join table1 tx on t2.pid = tx.pid
      group by t2.pid, tx.name, tx.code            

Author Comment

ID: 34989419


thank you very much for your replay. your sql true and easy, no problem.
"STUFF" : never used this command before.

have not tried your suggestion yet. Thank you for your return.

problem is solved

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In this article, we will see two different methods to recover deleted data. The first option will be using the transaction log to identify the operation and restore it in a specified section of the transaction log. The second option is simpler and c…
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.

622 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