Solved

show the sub-query in one line?

Posted on 2011-02-25
3
280 Views
Last Modified: 2012-05-11
Hi everyone;

I apologize for the bad english.

I have a two tables;

 1.Table

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

OR


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




Can I show the sub-query in one line?

Thank you for advice and assistance.

Hanifi GOKTAS
0
Comment
Question by:dmiebim
3 Comments
 
LVL 32

Accepted Solution

by:
ewangoya earned 500 total points
ID: 34983916
try this

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

Expert Comment

by:tigin44
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            
0
 

Author Comment

by:dmiebim
ID: 34989419

ewangoya;

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


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

problem is solved
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help Required 3 108
SQL Server - Set Field Values ito Zero Based on Related Table 4 40
Master DB with Masterkey 1 34
sql how to count case when 4 25
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

828 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