Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Display multiple row values in a single row. Expand / Collapse
Author
Message
Posted Monday, September 24, 2012 3:02 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, September 24, 2012 5:21 AM
Points: 2, Visits: 5
Hi,

Lets say, I have a table with Gender,Name and Age.


Gender Name Age
--------------------
Male John 52
Female Joe 33
Male Jim 42

I want the output to be like;

Male John 52 Jim 42
Female Joe 33

can anybody help me this on SQL SERVER 2003...??
Post #1363376
Posted Monday, September 24, 2012 3:56 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: 2 days ago @ 9:53 PM
Points: 3,438, Visits: 5,390
Like this:

DECLARE @Names TABLE
(Gender VARCHAR(6), Name VARCHAR(6), Age INT)

INSERT INTO @Names
SELECT 'Male','John', 52
UNION ALL SELECT 'Female','Joe', 33
UNION ALL SELECT 'Male','Jim',42

SELECT Gender, Names=
STUFF((
SELECT ' ' + Name + ' ' + CAST(Age AS VARCHAR)
FROM @Names b
WHERE a.Gender = b.Gender
FOR XML PATH(''), TYPE).value('.', 'VARCHAR(MAX)')
,1, 1, '')
FROM @Names a





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1363392
Posted Monday, September 24, 2012 4:26 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, December 1, 2014 7:30 AM
Points: 2,128, Visits: 5,565
My way is very similare to Dwain's way, but I also added the distinct key word so there will be only 2 rows. I also didn't use the stuff function:


DECLARE @Tbl TABLE (Gender char(6), Name varchar(20), Age tinyint)

INSERT INTO @Tbl (Gender, Name, Age)
SELECT 'Male', 'John', 52
UNION ALL
SELECT 'Female', 'Joe', 33
UNION ALL
SELECT 'Male', 'Jim', 42


select distinct Gender,
cast((select Name + ' ' + cast(Age as char(2)) + ' ' FROM @Tbl t1 where t1.Gender = t2.Gender for xml path('')) as varchar(max)) as Det
from @Tbl t2

Adi


--------------------------------------------------------------
To know how to ask questions and increase the chances of getting asnwers:
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #1363403
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse