?
Solved

show the sub-query in one line?

Posted on 2011-02-25
3
Medium Priority
?
285 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:
Ephraim Wangoya earned 2000 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Loops Section Overview

850 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