SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 SQL help
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

rupaka
Starting Member

4 Posts

Posted - 11/05/2010 :  16:31:47  Show Profile  Reply with Quote
How to i group and display out put of an SQL ( multiple ) into a single records

for eg

my records are

001 234 454 test 34
001 234 454 test2 34

How do i write an SQl to get output grouped them in single row


tkizer
Almighty SQL Goddess

USA
37316 Posts

Posted - 11/05/2010 :  16:33:01  Show Profile  Visit tkizer's Homepage  Reply with Quote
Show us how you want it to look.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

rupaka
Starting Member

4 Posts

Posted - 11/05/2010 :  16:44:28  Show Profile  Reply with Quote
My query returns records like this

ClA ClB ClC ClD
001 abc 456 test
001 abc 456 test1
001 abc 456 test2
001 abc 456 test3

i wnated a single record which can actually group all these

001 abc 456 test test1 test2 test3

or any xml output like..
001
abc
456
test
test1
test2
test3
Go to Top of Page

KlausEngel
Yak Posting Veteran

85 Posts

Posted - 11/05/2010 :  21:33:33  Show Profile  Reply with Quote
Try something like this:
SELECT
STUFF((SELECT s2.C1A + ' ' + s2.C1B + ' ' + s2.C1C + ' ' + s1.C1D FROM TableName AS s2 WHERE s2.C1D = s1.C1D FOR XML PATH('')), 1, 1, '') AS Entries
FROM TableName AS s1
ORDER BY s1.C1D
Go to Top of Page

rupaka
Starting Member

4 Posts

Posted - 11/07/2010 :  01:15:54  Show Profile  Reply with Quote
Thanks Guys !
Go to Top of Page

glendcruz
Yak Posting Veteran

Australia
60 Posts

Posted - 11/07/2010 :  01:16:42  Show Profile  Reply with Quote
DECLARE @T TABLE
(
CIA INT,
CIB VARCHAR(10),
CIC INT,
CID VARCHAR(10)
)
INSERT INTO @T
SELECT 001, 'abc', 456, 'test' UNION ALL
SELECT 001, 'abc', 456, 'test1' UNION ALL
SELECT 001, 'abc', 456, 'test2' UNION ALL
SELECT 001, 'abc', 456, 'test3'

select distinct '00'+ cast(j.cia as char(3))as 'cia',j.cib,j.cic ,
STUFF((SELECT TOP 100 PERCENT ' ' + s2.cid
FROM @T AS s2
WHERE s2.cia = j.cia
ORDER BY '' + s2.cid
FOR XML PATH('')
), 1, 1, '') AS 'All text added'
from @t j


(4 row(s) affected)
cia cib cic All text added
----- ---------- ----------- ---------------------
001 abc 456 test test1 test2 test3

(1 row(s) affected)



Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000