Solved

show the sub-query in one line?

Posted on 2011-02-25
3
277 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
Comment Utility
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
Comment Utility
     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
Comment Utility

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
how to fix this error 14 45
Sql query 34 16
SQL Transaction logs 8 6
How to simplify my SQL statement? 9 10
In this article I will describe the Backup & Restore 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.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

772 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

11 Experts available now in Live!

Get 1:1 Help Now